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