boot.h 7.8 KB

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