boot.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. * Copyright 2009 Intel Corporation; author H. Peter Anvin
  6. *
  7. * This file is part of the Linux kernel, and is made available under
  8. * the terms of the GNU General Public License version 2.
  9. *
  10. * ----------------------------------------------------------------------- */
  11. /*
  12. * Header file for the real-mode kernel code
  13. */
  14. #ifndef BOOT_BOOT_H
  15. #define BOOT_BOOT_H
  16. #define STACK_SIZE 512 /* Minimum number of bytes for stack */
  17. #ifndef __ASSEMBLY__
  18. #include <stdarg.h>
  19. #include <linux/types.h>
  20. #include <linux/edd.h>
  21. #include <asm/boot.h>
  22. #include <asm/setup.h>
  23. #include "bitops.h"
  24. #include <asm/cpufeature.h>
  25. #include <asm/processor-flags.h>
  26. #include "ctype.h"
  27. /* Useful macros */
  28. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  29. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
  30. extern struct setup_header hdr;
  31. extern struct boot_params boot_params;
  32. #define cpu_relax() asm volatile("rep; nop")
  33. /* Basic port I/O */
  34. static inline void outb(u8 v, u16 port)
  35. {
  36. asm volatile("outb %0,%1" : : "a" (v), "dN" (port));
  37. }
  38. static inline u8 inb(u16 port)
  39. {
  40. u8 v;
  41. asm volatile("inb %1,%0" : "=a" (v) : "dN" (port));
  42. return v;
  43. }
  44. static inline void outw(u16 v, u16 port)
  45. {
  46. asm volatile("outw %0,%1" : : "a" (v), "dN" (port));
  47. }
  48. static inline u16 inw(u16 port)
  49. {
  50. u16 v;
  51. asm volatile("inw %1,%0" : "=a" (v) : "dN" (port));
  52. return v;
  53. }
  54. static inline void outl(u32 v, u16 port)
  55. {
  56. asm volatile("outl %0,%1" : : "a" (v), "dN" (port));
  57. }
  58. static inline u32 inl(u32 port)
  59. {
  60. u32 v;
  61. asm volatile("inl %1,%0" : "=a" (v) : "dN" (port));
  62. return v;
  63. }
  64. static inline void io_delay(void)
  65. {
  66. const u16 DELAY_PORT = 0x80;
  67. asm volatile("outb %%al,%0" : : "dN" (DELAY_PORT));
  68. }
  69. /* These functions are used to reference data in other segments. */
  70. static inline u16 ds(void)
  71. {
  72. u16 seg;
  73. asm("movw %%ds,%0" : "=rm" (seg));
  74. return seg;
  75. }
  76. static inline void set_fs(u16 seg)
  77. {
  78. asm volatile("movw %0,%%fs" : : "rm" (seg));
  79. }
  80. static inline u16 fs(void)
  81. {
  82. u16 seg;
  83. asm volatile("movw %%fs,%0" : "=rm" (seg));
  84. return seg;
  85. }
  86. static inline void set_gs(u16 seg)
  87. {
  88. asm volatile("movw %0,%%gs" : : "rm" (seg));
  89. }
  90. static inline u16 gs(void)
  91. {
  92. u16 seg;
  93. asm volatile("movw %%gs,%0" : "=rm" (seg));
  94. return seg;
  95. }
  96. typedef unsigned int addr_t;
  97. static inline u8 rdfs8(addr_t addr)
  98. {
  99. u8 v;
  100. asm volatile("movb %%fs:%1,%0" : "=q" (v) : "m" (*(u8 *)addr));
  101. return v;
  102. }
  103. static inline u16 rdfs16(addr_t addr)
  104. {
  105. u16 v;
  106. asm volatile("movw %%fs:%1,%0" : "=r" (v) : "m" (*(u16 *)addr));
  107. return v;
  108. }
  109. static inline u32 rdfs32(addr_t addr)
  110. {
  111. u32 v;
  112. asm volatile("movl %%fs:%1,%0" : "=r" (v) : "m" (*(u32 *)addr));
  113. return v;
  114. }
  115. static inline void wrfs8(u8 v, addr_t addr)
  116. {
  117. asm volatile("movb %1,%%fs:%0" : "+m" (*(u8 *)addr) : "qi" (v));
  118. }
  119. static inline void wrfs16(u16 v, addr_t addr)
  120. {
  121. asm volatile("movw %1,%%fs:%0" : "+m" (*(u16 *)addr) : "ri" (v));
  122. }
  123. static inline void wrfs32(u32 v, addr_t addr)
  124. {
  125. asm volatile("movl %1,%%fs:%0" : "+m" (*(u32 *)addr) : "ri" (v));
  126. }
  127. static inline u8 rdgs8(addr_t addr)
  128. {
  129. u8 v;
  130. asm volatile("movb %%gs:%1,%0" : "=q" (v) : "m" (*(u8 *)addr));
  131. return v;
  132. }
  133. static inline u16 rdgs16(addr_t addr)
  134. {
  135. u16 v;
  136. asm volatile("movw %%gs:%1,%0" : "=r" (v) : "m" (*(u16 *)addr));
  137. return v;
  138. }
  139. static inline u32 rdgs32(addr_t addr)
  140. {
  141. u32 v;
  142. asm volatile("movl %%gs:%1,%0" : "=r" (v) : "m" (*(u32 *)addr));
  143. return v;
  144. }
  145. static inline void wrgs8(u8 v, addr_t addr)
  146. {
  147. asm volatile("movb %1,%%gs:%0" : "+m" (*(u8 *)addr) : "qi" (v));
  148. }
  149. static inline void wrgs16(u16 v, addr_t addr)
  150. {
  151. asm volatile("movw %1,%%gs:%0" : "+m" (*(u16 *)addr) : "ri" (v));
  152. }
  153. static inline void wrgs32(u32 v, addr_t addr)
  154. {
  155. asm volatile("movl %1,%%gs:%0" : "+m" (*(u32 *)addr) : "ri" (v));
  156. }
  157. /* Note: these only return true/false, not a signed return value! */
  158. static inline int memcmp(const void *s1, const void *s2, size_t len)
  159. {
  160. u8 diff;
  161. asm("repe; cmpsb; setnz %0"
  162. : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
  163. return diff;
  164. }
  165. static inline int memcmp_fs(const void *s1, addr_t s2, size_t len)
  166. {
  167. u8 diff;
  168. asm volatile("fs; repe; cmpsb; setnz %0"
  169. : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
  170. return diff;
  171. }
  172. static inline int memcmp_gs(const void *s1, addr_t s2, size_t len)
  173. {
  174. u8 diff;
  175. asm volatile("gs; repe; cmpsb; setnz %0"
  176. : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
  177. return diff;
  178. }
  179. /* Heap -- available for dynamic lists. */
  180. extern char _end[];
  181. extern char *HEAP;
  182. extern char *heap_end;
  183. #define RESET_HEAP() ((void *)( HEAP = _end ))
  184. static inline char *__get_heap(size_t s, size_t a, size_t n)
  185. {
  186. char *tmp;
  187. HEAP = (char *)(((size_t)HEAP+(a-1)) & ~(a-1));
  188. tmp = HEAP;
  189. HEAP += s*n;
  190. return tmp;
  191. }
  192. #define GET_HEAP(type, n) \
  193. ((type *)__get_heap(sizeof(type),__alignof__(type),(n)))
  194. static inline bool heap_free(size_t n)
  195. {
  196. return (int)(heap_end-HEAP) >= (int)n;
  197. }
  198. /* copy.S */
  199. void copy_to_fs(addr_t dst, void *src, size_t len);
  200. void *copy_from_fs(void *dst, addr_t src, size_t len);
  201. void copy_to_gs(addr_t dst, void *src, size_t len);
  202. void *copy_from_gs(void *dst, addr_t src, size_t len);
  203. void *memcpy(void *dst, void *src, size_t len);
  204. void *memset(void *dst, int c, size_t len);
  205. #define memcpy(d,s,l) __builtin_memcpy(d,s,l)
  206. #define memset(d,c,l) __builtin_memset(d,c,l)
  207. /* a20.c */
  208. int enable_a20(void);
  209. /* apm.c */
  210. int query_apm_bios(void);
  211. /* bioscall.c */
  212. struct biosregs {
  213. union {
  214. struct {
  215. u32 edi;
  216. u32 esi;
  217. u32 ebp;
  218. u32 _esp;
  219. u32 ebx;
  220. u32 edx;
  221. u32 ecx;
  222. u32 eax;
  223. u32 _fsgs;
  224. u32 _dses;
  225. u32 eflags;
  226. };
  227. struct {
  228. u16 di, hdi;
  229. u16 si, hsi;
  230. u16 bp, hbp;
  231. u16 _sp, _hsp;
  232. u16 bx, hbx;
  233. u16 dx, hdx;
  234. u16 cx, hcx;
  235. u16 ax, hax;
  236. u16 gs, fs;
  237. u16 es, ds;
  238. u16 flags, hflags;
  239. };
  240. struct {
  241. u8 dil, dih, edi2, edi3;
  242. u8 sil, sih, esi2, esi3;
  243. u8 bpl, bph, ebp2, ebp3;
  244. u8 _spl, _sph, _esp2, _esp3;
  245. u8 bl, bh, ebx2, ebx3;
  246. u8 dl, dh, edx2, edx3;
  247. u8 cl, ch, ecx2, ecx3;
  248. u8 al, ah, eax2, eax3;
  249. };
  250. };
  251. };
  252. void intcall(u8 int_no, const struct biosregs *ireg, struct biosregs *oreg);
  253. /* cmdline.c */
  254. int __cmdline_find_option(u32 cmdline_ptr, const char *option, char *buffer, int bufsize);
  255. int __cmdline_find_option_bool(u32 cmdline_ptr, const char *option);
  256. static inline int cmdline_find_option(const char *option, char *buffer, int bufsize)
  257. {
  258. return __cmdline_find_option(boot_params.hdr.cmd_line_ptr, option, buffer, bufsize);
  259. }
  260. static inline int cmdline_find_option_bool(const char *option)
  261. {
  262. return __cmdline_find_option_bool(boot_params.hdr.cmd_line_ptr, option);
  263. }
  264. /* cpu.c, cpucheck.c */
  265. struct cpu_features {
  266. int level; /* Family, or 64 for x86-64 */
  267. int model;
  268. u32 flags[NCAPINTS];
  269. };
  270. extern struct cpu_features cpu;
  271. int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr);
  272. int validate_cpu(void);
  273. /* early_serial_console.c */
  274. extern int early_serial_base;
  275. void console_init(void);
  276. /* edd.c */
  277. void query_edd(void);
  278. /* header.S */
  279. void __attribute__((noreturn)) die(void);
  280. /* mca.c */
  281. int query_mca(void);
  282. /* memory.c */
  283. int detect_memory(void);
  284. /* pm.c */
  285. void __attribute__((noreturn)) go_to_protected_mode(void);
  286. /* pmjump.S */
  287. void __attribute__((noreturn))
  288. protected_mode_jump(u32 entrypoint, u32 bootparams);
  289. /* printf.c */
  290. int sprintf(char *buf, const char *fmt, ...);
  291. int vsprintf(char *buf, const char *fmt, va_list args);
  292. int printf(const char *fmt, ...);
  293. /* regs.c */
  294. void initregs(struct biosregs *regs);
  295. /* string.c */
  296. int strcmp(const char *str1, const char *str2);
  297. int strncmp(const char *cs, const char *ct, size_t count);
  298. size_t strnlen(const char *s, size_t maxlen);
  299. unsigned int atou(const char *s);
  300. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base);
  301. /* tty.c */
  302. void puts(const char *);
  303. void putchar(int);
  304. int getchar(void);
  305. void kbd_flush(void);
  306. int getchar_timeout(void);
  307. /* video.c */
  308. void set_video(void);
  309. /* video-mode.c */
  310. int set_mode(u16 mode);
  311. int mode_defined(u16 mode);
  312. void probe_cards(int unsafe);
  313. /* video-vesa.c */
  314. void vesa_store_edid(void);
  315. #endif /* __ASSEMBLY__ */
  316. #endif /* BOOT_BOOT_H */