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_MAILBOX ((char*)0xe00)
31
32
33extern ulong __realmode_start;
34extern ulong __realmode_size;
35extern char realmode_enter;
36
37int realmode_setup(void)
38{
39 ulong realmode_start = (ulong)&__realmode_start + gd->reloc_off;
40 ulong realmode_size = (ulong)&__realmode_size;
41
42
43 if (realmode_size > (REALMODE_MAILBOX - (char *)REALMODE_BASE)) {
44 printf("realmode switch too large (%ld bytes, max is %d)\n",
45 realmode_size,
46 (REALMODE_MAILBOX - (char *)REALMODE_BASE));
47 return -1;
48 }
49
50 memcpy((char *)REALMODE_BASE, (void *)realmode_start, realmode_size);
51 asm("wbinvd\n");
52
53 return 0;
54}
55
56int enter_realmode(u16 seg, u16 off, struct pt_regs *in, struct pt_regs *out)
57{
58
59
60 if (bios_setup()) {
61 return -1;
62 }
63
64 if (realmode_setup()) {
65 return -1;
66 }
67
68 in->eip = off;
69 in->xcs = seg;
70 if (3>(in->esp & 0xffff)) {
71 printf("Warning: entering realmode with sp < 4 will fail\n");
72 }
73
74 memcpy(REALMODE_MAILBOX, in, sizeof(struct pt_regs));
75 asm("wbinvd\n");
76
77 __asm__ volatile (
78 "lcall $0x20,%0\n" : : "i" (&realmode_enter) );
79
80 asm("wbinvd\n");
81 memcpy(out, REALMODE_MAILBOX, sizeof(struct pt_regs));
82
83 return out->eax;
84}
85
86
87
88
89int enter_realmode_int(u8 lvl, struct pt_regs *in, struct pt_regs *out)
90{
91
92 writeb(0xcd, 0x700);
93 writeb(lvl, 0x701);
94 writeb(0xcb, 0x702);
95 asm("wbinvd\n");
96
97 enter_realmode(0x00, 0x700, in, out);
98
99 return out->eflags&1;
100}
101