1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <common.h>
25#include <asm/io.h>
26#include <asm/ptrace.h>
27#include <asm/realmode.h>
28
29
30#define REALMODE_BASE ((char*)0x7c0)
31#define REALMODE_MAILBOX ((char*)0xe00)
32
33
34extern char realmode_enter;
35
36int realmode_setup(void)
37{
38
39 if (i386boot_realmode_size > (REALMODE_MAILBOX-REALMODE_BASE)) {
40 printf("realmode switch too large (%ld bytes, max is %d)\n",
41 i386boot_realmode_size, (REALMODE_MAILBOX-REALMODE_BASE));
42 return -1;
43 }
44
45 memcpy(REALMODE_BASE, (void*)i386boot_realmode, i386boot_realmode_size);
46 asm("wbinvd\n");
47
48 return 0;
49}
50
51int enter_realmode(u16 seg, u16 off, struct pt_regs *in, struct pt_regs *out)
52{
53
54
55 if (bios_setup()) {
56 return -1;
57 }
58
59 if (realmode_setup()) {
60 return -1;
61 }
62
63 in->eip = off;
64 in->xcs = seg;
65 if (3>(in->esp & 0xffff)) {
66 printf("Warning: entering realmode with sp < 4 will fail\n");
67 }
68
69 memcpy(REALMODE_MAILBOX, in, sizeof(struct pt_regs));
70 asm("wbinvd\n");
71
72 __asm__ volatile (
73 "lcall $0x20,%0\n" : : "i" (&realmode_enter) );
74
75 asm("wbinvd\n");
76 memcpy(out, REALMODE_MAILBOX, sizeof(struct pt_regs));
77
78 return out->eax;
79}
80
81
82
83
84int enter_realmode_int(u8 lvl, struct pt_regs *in, struct pt_regs *out)
85{
86
87 writeb(0xcd, 0x700);
88 writeb(lvl, 0x701);
89 writeb(0xcb, 0x702);
90 asm("wbinvd\n");
91
92 enter_realmode(0x00, 0x700, in, out);
93
94 return out->eflags&1;
95}
96