boot.h 7.8 KB

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