boot.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. *
  6. * This file is part of the Linux kernel, and is made available under
  7. * the terms of the GNU General Public License version 2.
  8. *
  9. * ----------------------------------------------------------------------- */
  10. /*
  11. * arch/i386/boot/boot.h
  12. *
  13. * Header file for the real-mode kernel code
  14. */
  15. #ifndef BOOT_BOOT_H
  16. #define BOOT_BOOT_H
  17. #define STACK_SIZE 512 /* Minimum number of bytes for stack */
  18. #ifndef __ASSEMBLY__
  19. #include <stdarg.h>
  20. #include <linux/types.h>
  21. #include <linux/edd.h>
  22. #include <asm/boot.h>
  23. #include <asm/setup.h>
  24. /* Useful macros */
  25. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  26. extern struct setup_header hdr;
  27. extern struct boot_params boot_params;
  28. /* Basic port I/O */
  29. static inline void outb(u8 v, u16 port)
  30. {
  31. asm volatile("outb %0,%1" : : "a" (v), "dN" (port));
  32. }
  33. static inline u8 inb(u16 port)
  34. {
  35. u8 v;
  36. asm volatile("inb %1,%0" : "=a" (v) : "dN" (port));
  37. return v;
  38. }
  39. static inline void outw(u16 v, u16 port)
  40. {
  41. asm volatile("outw %0,%1" : : "a" (v), "dN" (port));
  42. }
  43. static inline u16 inw(u16 port)
  44. {
  45. u16 v;
  46. asm volatile("inw %1,%0" : "=a" (v) : "dN" (port));
  47. return v;
  48. }
  49. static inline void outl(u32 v, u16 port)
  50. {
  51. asm volatile("outl %0,%1" : : "a" (v), "dN" (port));
  52. }
  53. static inline u32 inl(u32 port)
  54. {
  55. u32 v;
  56. asm volatile("inl %1,%0" : "=a" (v) : "dN" (port));
  57. return v;
  58. }
  59. static inline void io_delay(void)
  60. {
  61. const u16 DELAY_PORT = 0x80;
  62. asm volatile("outb %%al,%0" : : "dN" (DELAY_PORT));
  63. }
  64. /* These functions are used to reference data in other segments. */
  65. static inline u16 ds(void)
  66. {
  67. u16 seg;
  68. asm("movw %%ds,%0" : "=rm" (seg));
  69. return seg;
  70. }
  71. static inline void set_fs(u16 seg)
  72. {
  73. asm volatile("movw %0,%%fs" : : "rm" (seg));
  74. }
  75. static inline u16 fs(void)
  76. {
  77. u16 seg;
  78. asm volatile("movw %%fs,%0" : "=rm" (seg));
  79. return seg;
  80. }
  81. static inline void set_gs(u16 seg)
  82. {
  83. asm volatile("movw %0,%%gs" : : "rm" (seg));
  84. }
  85. static inline u16 gs(void)
  86. {
  87. u16 seg;
  88. asm volatile("movw %%gs,%0" : "=rm" (seg));
  89. return seg;
  90. }
  91. typedef unsigned int addr_t;
  92. static inline u8 rdfs8(addr_t addr)
  93. {
  94. u8 v;
  95. asm volatile("movb %%fs:%1,%0" : "=q" (v) : "m" (*(u8 *)addr));
  96. return v;
  97. }
  98. static inline u16 rdfs16(addr_t addr)
  99. {
  100. u16 v;
  101. asm volatile("movw %%fs:%1,%0" : "=r" (v) : "m" (*(u16 *)addr));
  102. return v;
  103. }
  104. static inline u32 rdfs32(addr_t addr)
  105. {
  106. u32 v;
  107. asm volatile("movl %%fs:%1,%0" : "=r" (v) : "m" (*(u32 *)addr));
  108. return v;
  109. }
  110. static inline void wrfs8(u8 v, addr_t addr)
  111. {
  112. asm volatile("movb %1,%%fs:%0" : "+m" (*(u8 *)addr) : "qi" (v));
  113. }
  114. static inline void wrfs16(u16 v, addr_t addr)
  115. {
  116. asm volatile("movw %1,%%fs:%0" : "+m" (*(u16 *)addr) : "ri" (v));
  117. }
  118. static inline void wrfs32(u32 v, addr_t addr)
  119. {
  120. asm volatile("movl %1,%%fs:%0" : "+m" (*(u32 *)addr) : "ri" (v));
  121. }
  122. static inline u8 rdgs8(addr_t addr)
  123. {
  124. u8 v;
  125. asm volatile("movb %%gs:%1,%0" : "=q" (v) : "m" (*(u8 *)addr));
  126. return v;
  127. }
  128. static inline u16 rdgs16(addr_t addr)
  129. {
  130. u16 v;
  131. asm volatile("movw %%gs:%1,%0" : "=r" (v) : "m" (*(u16 *)addr));
  132. return v;
  133. }
  134. static inline u32 rdgs32(addr_t addr)
  135. {
  136. u32 v;
  137. asm volatile("movl %%gs:%1,%0" : "=r" (v) : "m" (*(u32 *)addr));
  138. return v;
  139. }
  140. static inline void wrgs8(u8 v, addr_t addr)
  141. {
  142. asm volatile("movb %1,%%gs:%0" : "+m" (*(u8 *)addr) : "qi" (v));
  143. }
  144. static inline void wrgs16(u16 v, addr_t addr)
  145. {
  146. asm volatile("movw %1,%%gs:%0" : "+m" (*(u16 *)addr) : "ri" (v));
  147. }
  148. static inline void wrgs32(u32 v, addr_t addr)
  149. {
  150. asm volatile("movl %1,%%gs:%0" : "+m" (*(u32 *)addr) : "ri" (v));
  151. }
  152. /* Note: these only return true/false, not a signed return value! */
  153. static inline int memcmp(const void *s1, const void *s2, size_t len)
  154. {
  155. u8 diff;
  156. asm("repe; cmpsb; setnz %0"
  157. : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
  158. return diff;
  159. }
  160. static inline int memcmp_fs(const void *s1, addr_t s2, size_t len)
  161. {
  162. u8 diff;
  163. asm volatile("fs; repe; cmpsb; setnz %0"
  164. : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
  165. return diff;
  166. }
  167. static inline int memcmp_gs(const void *s1, addr_t s2, size_t len)
  168. {
  169. u8 diff;
  170. asm volatile("gs; repe; cmpsb; setnz %0"
  171. : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
  172. return diff;
  173. }
  174. static inline int isdigit(int ch)
  175. {
  176. return (ch >= '0') && (ch <= '9');
  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. /* cmdline.c */
  211. int cmdline_find_option(const char *option, char *buffer, int bufsize);
  212. int cmdline_find_option_bool(const char *option);
  213. /* cpu.c, cpucheck.c */
  214. int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr);
  215. int validate_cpu(void);
  216. /* edd.c */
  217. void query_edd(void);
  218. /* header.S */
  219. void __attribute__((noreturn)) die(void);
  220. /* mca.c */
  221. int query_mca(void);
  222. /* memory.c */
  223. int detect_memory(void);
  224. /* pm.c */
  225. void __attribute__((noreturn)) go_to_protected_mode(void);
  226. /* pmjump.S */
  227. void __attribute__((noreturn))
  228. protected_mode_jump(u32 entrypoint, u32 bootparams);
  229. /* printf.c */
  230. int sprintf(char *buf, const char *fmt, ...);
  231. int vsprintf(char *buf, const char *fmt, va_list args);
  232. int printf(const char *fmt, ...);
  233. /* string.c */
  234. int strcmp(const char *str1, const char *str2);
  235. size_t strnlen(const char *s, size_t maxlen);
  236. unsigned int atou(const char *s);
  237. /* tty.c */
  238. void puts(const char *);
  239. void putchar(int);
  240. int getchar(void);
  241. void kbd_flush(void);
  242. int getchar_timeout(void);
  243. /* video.c */
  244. void set_video(void);
  245. /* video-vesa.c */
  246. void vesa_store_edid(void);
  247. /* voyager.c */
  248. int query_voyager(void);
  249. #endif /* __ASSEMBLY__ */
  250. #endif /* BOOT_BOOT_H */