blob: 98239d2658f27b3ed3494ea1b0ad3d6cd0a01624 [file] [log] [blame]
H. Peter Anvin62bd0332007-07-11 12:18:39 -07001/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright 2007 rPath, Inc. - All Rights Reserved
H. Peter Anvin7a734e72009-04-01 18:08:28 -07005 * Copyright 2009 Intel Corporation; author H. Peter Anvin
H. Peter Anvin62bd0332007-07-11 12:18:39 -07006 *
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/*
H. Peter Anvin62bd0332007-07-11 12:18:39 -070013 * Header file for the real-mode kernel code
14 */
15
16#ifndef BOOT_BOOT_H
17#define BOOT_BOOT_H
18
H. Peter Anvin6b6815c2007-10-25 16:11:33 -070019#define STACK_SIZE 512 /* Minimum number of bytes for stack */
20
H. Peter Anvin62bd0332007-07-11 12:18:39 -070021#ifndef __ASSEMBLY__
22
23#include <stdarg.h>
24#include <linux/types.h>
25#include <linux/edd.h>
26#include <asm/boot.h>
H. Peter Anvinfa76dab2007-10-23 22:37:25 +020027#include <asm/setup.h>
H. Peter Anvinc2dcfde2008-08-13 13:14:22 -070028#include "bitops.h"
29#include <asm/cpufeature.h>
H. Peter Anvin7a734e72009-04-01 18:08:28 -070030#include <asm/processor-flags.h>
Joerg Roedel7b277182008-08-13 10:07:05 +020031
H. Peter Anvin62bd0332007-07-11 12:18:39 -070032/* Useful macros */
33#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
34
Andi Kleen1b726912008-08-18 09:06:01 +020035#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
36
H. Peter Anvin62bd0332007-07-11 12:18:39 -070037extern struct setup_header hdr;
38extern struct boot_params boot_params;
39
40/* Basic port I/O */
41static inline void outb(u8 v, u16 port)
42{
43 asm volatile("outb %0,%1" : : "a" (v), "dN" (port));
44}
45static inline u8 inb(u16 port)
46{
47 u8 v;
48 asm volatile("inb %1,%0" : "=a" (v) : "dN" (port));
49 return v;
50}
51
52static inline void outw(u16 v, u16 port)
53{
54 asm volatile("outw %0,%1" : : "a" (v), "dN" (port));
55}
56static inline u16 inw(u16 port)
57{
58 u16 v;
59 asm volatile("inw %1,%0" : "=a" (v) : "dN" (port));
60 return v;
61}
62
63static inline void outl(u32 v, u16 port)
64{
H. Peter Anvin5593eaa2007-07-14 16:47:13 -070065 asm volatile("outl %0,%1" : : "a" (v), "dN" (port));
H. Peter Anvin62bd0332007-07-11 12:18:39 -070066}
67static inline u32 inl(u32 port)
68{
69 u32 v;
70 asm volatile("inl %1,%0" : "=a" (v) : "dN" (port));
71 return v;
72}
73
74static 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
82static inline u16 ds(void)
83{
84 u16 seg;
85 asm("movw %%ds,%0" : "=rm" (seg));
86 return seg;
87}
88
89static inline void set_fs(u16 seg)
90{
91 asm volatile("movw %0,%%fs" : : "rm" (seg));
92}
93static inline u16 fs(void)
94{
95 u16 seg;
H. Peter Anvinb0151242007-08-22 16:28:01 -070096 asm volatile("movw %%fs,%0" : "=rm" (seg));
H. Peter Anvin62bd0332007-07-11 12:18:39 -070097 return seg;
98}
99
100static inline void set_gs(u16 seg)
101{
102 asm volatile("movw %0,%%gs" : : "rm" (seg));
103}
104static inline u16 gs(void)
105{
106 u16 seg;
H. Peter Anvinb0151242007-08-22 16:28:01 -0700107 asm volatile("movw %%gs,%0" : "=rm" (seg));
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700108 return seg;
109}
110
111typedef unsigned int addr_t;
112
113static inline u8 rdfs8(addr_t addr)
114{
115 u8 v;
H. Peter Anvin811a0ff2008-01-30 13:33:02 +0100116 asm volatile("movb %%fs:%1,%0" : "=q" (v) : "m" (*(u8 *)addr));
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700117 return v;
118}
119static inline u16 rdfs16(addr_t addr)
120{
121 u16 v;
H. Peter Anvinb0151242007-08-22 16:28:01 -0700122 asm volatile("movw %%fs:%1,%0" : "=r" (v) : "m" (*(u16 *)addr));
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700123 return v;
124}
125static inline u32 rdfs32(addr_t addr)
126{
127 u32 v;
H. Peter Anvinb0151242007-08-22 16:28:01 -0700128 asm volatile("movl %%fs:%1,%0" : "=r" (v) : "m" (*(u32 *)addr));
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700129 return v;
130}
131
132static inline void wrfs8(u8 v, addr_t addr)
133{
H. Peter Anvin811a0ff2008-01-30 13:33:02 +0100134 asm volatile("movb %1,%%fs:%0" : "+m" (*(u8 *)addr) : "qi" (v));
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700135}
136static inline void wrfs16(u16 v, addr_t addr)
137{
H. Peter Anvin811a0ff2008-01-30 13:33:02 +0100138 asm volatile("movw %1,%%fs:%0" : "+m" (*(u16 *)addr) : "ri" (v));
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700139}
140static inline void wrfs32(u32 v, addr_t addr)
141{
H. Peter Anvin811a0ff2008-01-30 13:33:02 +0100142 asm volatile("movl %1,%%fs:%0" : "+m" (*(u32 *)addr) : "ri" (v));
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700143}
144
145static inline u8 rdgs8(addr_t addr)
146{
147 u8 v;
H. Peter Anvin811a0ff2008-01-30 13:33:02 +0100148 asm volatile("movb %%gs:%1,%0" : "=q" (v) : "m" (*(u8 *)addr));
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700149 return v;
150}
151static inline u16 rdgs16(addr_t addr)
152{
153 u16 v;
H. Peter Anvinb0151242007-08-22 16:28:01 -0700154 asm volatile("movw %%gs:%1,%0" : "=r" (v) : "m" (*(u16 *)addr));
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700155 return v;
156}
157static inline u32 rdgs32(addr_t addr)
158{
159 u32 v;
H. Peter Anvinb0151242007-08-22 16:28:01 -0700160 asm volatile("movl %%gs:%1,%0" : "=r" (v) : "m" (*(u32 *)addr));
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700161 return v;
162}
163
164static inline void wrgs8(u8 v, addr_t addr)
165{
H. Peter Anvin811a0ff2008-01-30 13:33:02 +0100166 asm volatile("movb %1,%%gs:%0" : "+m" (*(u8 *)addr) : "qi" (v));
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700167}
168static inline void wrgs16(u16 v, addr_t addr)
169{
H. Peter Anvin811a0ff2008-01-30 13:33:02 +0100170 asm volatile("movw %1,%%gs:%0" : "+m" (*(u16 *)addr) : "ri" (v));
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700171}
172static inline void wrgs32(u32 v, addr_t addr)
173{
H. Peter Anvin811a0ff2008-01-30 13:33:02 +0100174 asm volatile("movl %1,%%gs:%0" : "+m" (*(u32 *)addr) : "ri" (v));
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700175}
176
177/* Note: these only return true/false, not a signed return value! */
178static 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
186static inline int memcmp_fs(const void *s1, addr_t s2, size_t len)
187{
188 u8 diff;
H. Peter Anvinb0151242007-08-22 16:28:01 -0700189 asm volatile("fs; repe; cmpsb; setnz %0"
190 : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700191 return diff;
192}
193static inline int memcmp_gs(const void *s1, addr_t s2, size_t len)
194{
195 u8 diff;
H. Peter Anvinb0151242007-08-22 16:28:01 -0700196 asm volatile("gs; repe; cmpsb; setnz %0"
197 : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700198 return diff;
199}
200
201static inline int isdigit(int ch)
202{
203 return (ch >= '0') && (ch <= '9');
204}
205
206/* Heap -- available for dynamic lists. */
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700207extern char _end[];
208extern char *HEAP;
209extern char *heap_end;
210#define RESET_HEAP() ((void *)( HEAP = _end ))
211static 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
H. Peter Anvine6e1ace2007-10-25 16:09:38 -0700223static inline bool heap_free(size_t n)
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700224{
H. Peter Anvine6e1ace2007-10-25 16:09:38 -0700225 return (int)(heap_end-HEAP) >= (int)n;
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700226}
227
228/* copy.S */
229
230void copy_to_fs(addr_t dst, void *src, size_t len);
231void *copy_from_fs(void *dst, addr_t src, size_t len);
232void copy_to_gs(addr_t dst, void *src, size_t len);
233void *copy_from_gs(void *dst, addr_t src, size_t len);
234void *memcpy(void *dst, void *src, size_t len);
235void *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 */
241int enable_a20(void);
242
243/* apm.c */
244int query_apm_bios(void);
245
H. Peter Anvin7a734e72009-04-01 18:08:28 -0700246/* bioscall.c */
247struct 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};
287void intcall(u8 int_no, const struct biosregs *ireg, struct biosregs *oreg);
288
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700289/* cmdline.c */
290int cmdline_find_option(const char *option, char *buffer, int bufsize);
H. Peter Anvin70d8abf2008-01-30 13:33:03 +0100291int cmdline_find_option_bool(const char *option);
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700292
293/* cpu.c, cpucheck.c */
Joerg Roedel7b277182008-08-13 10:07:05 +0200294struct cpu_features {
295 int level; /* Family, or 64 for x86-64 */
296 int model;
297 u32 flags[NCAPINTS];
298};
299extern struct cpu_features cpu;
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700300int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr);
301int validate_cpu(void);
302
303/* edd.c */
304void query_edd(void);
305
306/* header.S */
307void __attribute__((noreturn)) die(void);
308
309/* mca.c */
310int query_mca(void);
311
312/* memory.c */
313int detect_memory(void);
314
315/* pm.c */
316void __attribute__((noreturn)) go_to_protected_mode(void);
317
318/* pmjump.S */
319void __attribute__((noreturn))
320 protected_mode_jump(u32 entrypoint, u32 bootparams);
321
322/* printf.c */
323int sprintf(char *buf, const char *fmt, ...);
324int vsprintf(char *buf, const char *fmt, va_list args);
325int printf(const char *fmt, ...);
326
H. Peter Anvin7a734e72009-04-01 18:08:28 -0700327/* regs.c */
328void initregs(struct biosregs *regs);
329
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700330/* string.c */
331int strcmp(const char *str1, const char *str2);
332size_t strnlen(const char *s, size_t maxlen);
333unsigned int atou(const char *s);
334
335/* tty.c */
336void puts(const char *);
337void putchar(int);
338int getchar(void);
339void kbd_flush(void);
340int getchar_timeout(void);
341
342/* video.c */
343void set_video(void);
344
Pavel Macheke44b7b72008-04-10 23:28:10 +0200345/* video-mode.c */
346int set_mode(u16 mode);
347int mode_defined(u16 mode);
348void probe_cards(int unsafe);
349
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700350/* video-vesa.c */
351void vesa_store_edid(void);
352
H. Peter Anvin62bd0332007-07-11 12:18:39 -0700353#endif /* __ASSEMBLY__ */
354
355#endif /* BOOT_BOOT_H */