boot.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 "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 int memcmp(const void *s1, const void *s2, size_t len)
  158. {
  159. u8 diff;
  160. asm("repe; cmpsb; setnz %0"
  161. : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
  162. return diff;
  163. }
  164. static inline int memcmp_fs(const void *s1, addr_t s2, size_t len)
  165. {
  166. u8 diff;
  167. asm volatile("fs; repe; cmpsb; setnz %0"
  168. : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
  169. return diff;
  170. }
  171. static inline int memcmp_gs(const void *s1, addr_t s2, size_t len)
  172. {
  173. u8 diff;
  174. asm volatile("gs; repe; cmpsb; setnz %0"
  175. : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
  176. return diff;
  177. }
  178. /* Heap -- available for dynamic lists. */
  179. extern char _end[];
  180. extern char *HEAP;
  181. extern char *heap_end;
  182. #define RESET_HEAP() ((void *)( HEAP = _end ))
  183. static inline char *__get_heap(size_t s, size_t a, size_t n)
  184. {
  185. char *tmp;
  186. HEAP = (char *)(((size_t)HEAP+(a-1)) & ~(a-1));
  187. tmp = HEAP;
  188. HEAP += s*n;
  189. return tmp;
  190. }
  191. #define GET_HEAP(type, n) \
  192. ((type *)__get_heap(sizeof(type),__alignof__(type),(n)))
  193. static inline bool heap_free(size_t n)
  194. {
  195. return (int)(heap_end-HEAP) >= (int)n;
  196. }
  197. /* copy.S */
  198. void copy_to_fs(addr_t dst, void *src, size_t len);
  199. void *copy_from_fs(void *dst, addr_t src, size_t len);
  200. void copy_to_gs(addr_t dst, void *src, size_t len);
  201. void *copy_from_gs(void *dst, addr_t src, size_t len);
  202. void *memcpy(void *dst, void *src, size_t len);
  203. void *memset(void *dst, int c, size_t len);
  204. #define memcpy(d,s,l) __builtin_memcpy(d,s,l)
  205. #define memset(d,c,l) __builtin_memset(d,c,l)
  206. /* a20.c */
  207. int enable_a20(void);
  208. /* apm.c */
  209. int query_apm_bios(void);
  210. /* bioscall.c */
  211. struct biosregs {
  212. union {
  213. struct {
  214. u32 edi;
  215. u32 esi;
  216. u32 ebp;
  217. u32 _esp;
  218. u32 ebx;
  219. u32 edx;
  220. u32 ecx;
  221. u32 eax;
  222. u32 _fsgs;
  223. u32 _dses;
  224. u32 eflags;
  225. };
  226. struct {
  227. u16 di, hdi;
  228. u16 si, hsi;
  229. u16 bp, hbp;
  230. u16 _sp, _hsp;
  231. u16 bx, hbx;
  232. u16 dx, hdx;
  233. u16 cx, hcx;
  234. u16 ax, hax;
  235. u16 gs, fs;
  236. u16 es, ds;
  237. u16 flags, hflags;
  238. };
  239. struct {
  240. u8 dil, dih, edi2, edi3;
  241. u8 sil, sih, esi2, esi3;
  242. u8 bpl, bph, ebp2, ebp3;
  243. u8 _spl, _sph, _esp2, _esp3;
  244. u8 bl, bh, ebx2, ebx3;
  245. u8 dl, dh, edx2, edx3;
  246. u8 cl, ch, ecx2, ecx3;
  247. u8 al, ah, eax2, eax3;
  248. };
  249. };
  250. };
  251. void intcall(u8 int_no, const struct biosregs *ireg, struct biosregs *oreg);
  252. /* cmdline.c */
  253. int __cmdline_find_option(unsigned long cmdline_ptr, const char *option, char *buffer, int bufsize);
  254. int __cmdline_find_option_bool(unsigned long cmdline_ptr, const char *option);
  255. static inline int cmdline_find_option(const char *option, char *buffer, int bufsize)
  256. {
  257. unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
  258. if (cmd_line_ptr >= 0x100000)
  259. return -1; /* inaccessible */
  260. return __cmdline_find_option(cmd_line_ptr, option, buffer, bufsize);
  261. }
  262. static inline int cmdline_find_option_bool(const char *option)
  263. {
  264. unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
  265. if (cmd_line_ptr >= 0x100000)
  266. return -1; /* inaccessible */
  267. return __cmdline_find_option_bool(cmd_line_ptr, option);
  268. }
  269. /* cpu.c, cpucheck.c */
  270. int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr);
  271. int validate_cpu(void);
  272. /* early_serial_console.c */
  273. extern int early_serial_base;
  274. void console_init(void);
  275. /* edd.c */
  276. void query_edd(void);
  277. /* header.S */
  278. void __attribute__((noreturn)) die(void);
  279. /* mca.c */
  280. int query_mca(void);
  281. /* memory.c */
  282. int detect_memory(void);
  283. /* pm.c */
  284. void __attribute__((noreturn)) go_to_protected_mode(void);
  285. /* pmjump.S */
  286. void __attribute__((noreturn))
  287. protected_mode_jump(u32 entrypoint, u32 bootparams);
  288. /* printf.c */
  289. int sprintf(char *buf, const char *fmt, ...);
  290. int vsprintf(char *buf, const char *fmt, va_list args);
  291. int printf(const char *fmt, ...);
  292. /* regs.c */
  293. void initregs(struct biosregs *regs);
  294. /* string.c */
  295. int strcmp(const char *str1, const char *str2);
  296. int strncmp(const char *cs, const char *ct, size_t count);
  297. size_t strnlen(const char *s, size_t maxlen);
  298. unsigned int atou(const char *s);
  299. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base);
  300. size_t strlen(const char *s);
  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 */