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