Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / system.c
1 // Handler for int 0x15 "system" calls
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "biosvar.h" // GET_GLOBAL
9 #include "bregs.h" // struct bregs
10 #include "hw/pic.h" // pic_reset
11 #include "malloc.h" // LegacyRamSize
12 #include "memmap.h" // E820_RAM
13 #include "output.h" // debug_enter
14 #include "string.h" // memcpy_far
15 #include "util.h" // handle_1553
16 #include "x86.h" // set_a20
17
18 static void
19 handle_152400(struct bregs *regs)
20 {
21     set_a20(0);
22     set_code_success(regs);
23 }
24
25 static void
26 handle_152401(struct bregs *regs)
27 {
28     set_a20(1);
29     set_code_success(regs);
30 }
31
32 static void
33 handle_152402(struct bregs *regs)
34 {
35     regs->al = get_a20();
36     set_code_success(regs);
37 }
38
39 static void
40 handle_152403(struct bregs *regs)
41 {
42     regs->bx = 3;
43     set_code_success(regs);
44 }
45
46 static void
47 handle_1524XX(struct bregs *regs)
48 {
49     set_code_unimplemented(regs, RET_EUNSUPPORTED);
50 }
51
52 static void
53 handle_1524(struct bregs *regs)
54 {
55     switch (regs->al) {
56     case 0x00: handle_152400(regs); break;
57     case 0x01: handle_152401(regs); break;
58     case 0x02: handle_152402(regs); break;
59     case 0x03: handle_152403(regs); break;
60     default:   handle_1524XX(regs); break;
61     }
62 }
63
64 // removable media eject
65 static void
66 handle_1552(struct bregs *regs)
67 {
68     set_code_success(regs);
69 }
70
71 static void
72 handle_1587(struct bregs *regs)
73 {
74     // +++ should probably have descriptor checks
75     // +++ should have exception handlers
76
77     u8 prev_a20_enable = set_a20(1); // enable A20 line
78
79     // 128K max of transfer on 386+ ???
80     // source == destination ???
81
82     // ES:SI points to descriptor table
83     // offset   use     initially  comments
84     // ==============================================
85     // 00..07   Unused  zeros      Null descriptor
86     // 08..0f   GDT     zeros      filled in by BIOS
87     // 10..17   source  ssssssss   source of data
88     // 18..1f   dest    dddddddd   destination of data
89     // 20..27   CS      zeros      filled in by BIOS
90     // 28..2f   SS      zeros      filled in by BIOS
91
92 // check for access rights of source & dest here
93
94     // Initialize GDT descriptor
95     u64 *gdt_far = (void*)(regs->si + 0);
96     u16 gdt_seg = regs->es;
97     u32 loc = (u32)MAKE_FLATPTR(gdt_seg, gdt_far);
98     SET_FARVAR(gdt_seg, gdt_far[1], GDT_DATA | GDT_LIMIT((6*sizeof(u64))-1)
99                | GDT_BASE(loc));
100     // Initialize CS descriptor
101     u64 lim = GDT_LIMIT(0x0ffff);
102     if (in_post())
103         lim = GDT_GRANLIMIT(0xffffffff);
104     SET_FARVAR(gdt_seg, gdt_far[4], GDT_CODE | lim | GDT_BASE(BUILD_BIOS_ADDR));
105     // Initialize SS descriptor
106     loc = (u32)MAKE_FLATPTR(GET_SEG(SS), 0);
107     SET_FARVAR(gdt_seg, gdt_far[5], GDT_DATA | lim | GDT_BASE(loc));
108
109     SET_SEG(ES, gdt_seg);
110     u16 count = regs->cx, si = 0, di = 0;
111     asm volatile(
112         // Load new descriptor tables
113         "  lgdtw %%es:(1<<3)(%%eax)\n"
114         "  lidtw %%cs:pmode_IDT_info\n"
115
116         // Enable protected mode
117         "  movl %%cr0, %%eax\n"
118         "  orl $" __stringify(CR0_PE) ", %%eax\n"
119         "  movl %%eax, %%cr0\n"
120
121         // far jump to flush CPU queue after transition to protected mode
122         "  ljmpw $(4<<3), $1f\n"
123
124         // GDT points to valid descriptor table, now load DS, ES
125         "1:movw $(2<<3), %%ax\n" // 2nd descriptor in table, TI=GDT, RPL=00
126         "  movw %%ax, %%ds\n"
127         "  movw $(3<<3), %%ax\n" // 3rd descriptor in table, TI=GDT, RPL=00
128         "  movw %%ax, %%es\n"
129
130         // memcpy CX words using 32bit memcpy if applicable
131         "  testw $1, %%cx\n"
132         "  jnz 3f\n"
133         "  shrw $1, %%cx\n"
134         "  rep movsl %%ds:(%%si), %%es:(%%di)\n"
135
136         // Restore DS and ES segment limits to 0xffff
137         "2:movw $(5<<3), %%ax\n" // 5th descriptor in table (SS)
138         "  movw %%ax, %%ds\n"
139         "  movw %%ax, %%es\n"
140
141         // Disable protected mode
142         "  movl %%cr0, %%eax\n"
143         "  andl $~" __stringify(CR0_PE) ", %%eax\n"
144         "  movl %%eax, %%cr0\n"
145
146         // far jump to flush CPU queue after transition to real mode
147         "  ljmpw $" __stringify(SEG_BIOS) ", $4f\n"
148
149         // Slower 16bit copy method
150         "3:rep movsw %%ds:(%%si), %%es:(%%di)\n"
151         "  jmp 2b\n"
152
153         // restore IDT to normal real-mode defaults
154         "4:lidtw %%cs:rmode_IDT_info\n"
155
156         // Restore %ds (from %ss)
157         "  movw %%ss, %%ax\n"
158         "  movw %%ax, %%ds\n"
159         : "+a" (gdt_far), "+c"(count), "+m" (__segment_ES)
160         : "S" (si), "D" (di)
161         : "cc");
162
163     set_a20(prev_a20_enable);
164
165     set_code_success(regs);
166 }
167
168 // Get the amount of extended memory (above 1M)
169 static void
170 handle_1588(struct bregs *regs)
171 {
172     u32 rs = GET_GLOBAL(LegacyRamSize);
173
174     // According to Ralf Brown's interrupt the limit should be 15M,
175     // but real machines mostly return max. 63M.
176     if (rs > 64*1024*1024)
177         regs->ax = 63 * 1024;
178     else
179         regs->ax = (rs - 1*1024*1024) / 1024;
180     set_success(regs);
181 }
182
183 // Switch to protected mode
184 void VISIBLE16
185 handle_1589(struct bregs *regs)
186 {
187     debug_enter(regs, DEBUG_HDL_15);
188     set_a20(1);
189
190     pic_reset(regs->bl, regs->bh);
191
192     u64 *gdt_far = (void*)(regs->si + 0);
193     u16 gdt_seg = regs->es;
194     SET_FARVAR(gdt_seg, gdt_far[7], GDT_CODE | GDT_LIMIT(BUILD_BIOS_SIZE-1)
195                | GDT_BASE(BUILD_BIOS_ADDR));
196
197     regs->ds = 3<<3; // 3rd gdt descriptor is %ds
198     regs->es = 4<<3; // 4th gdt descriptor is %es
199     regs->code.seg = 6<<3; // 6th gdt descriptor is %cs
200
201     set_code_success(regs);
202
203     asm volatile(
204         // Load new descriptor tables
205         "  lgdtw %%es:(1<<3)(%%si)\n"
206         "  lidtw %%es:(2<<3)(%%si)\n"
207
208         // Enable protected mode
209         "  movl %%cr0, %%eax\n"
210         "  orl $" __stringify(CR0_PE) ", %%eax\n"
211         "  movl %%eax, %%cr0\n"
212
213         // far jump to flush CPU queue after transition to protected mode
214         "  ljmpw $(7<<3), $1f\n"
215
216         // GDT points to valid descriptor table, now load SS
217         "1:movw $(5<<3), %%ax\n"
218         "  movw %%ax, %%ds\n"
219         "  movw %%ax, %%ss\n"
220         :
221         : "S"(gdt_far), "m" (__segment_ES)
222         : "eax", "cc");
223 }
224
225 // Device busy interrupt.  Called by Int 16h when no key available
226 static void
227 handle_1590(struct bregs *regs)
228 {
229 }
230
231 // Interrupt complete.  Called by Int 16h when key becomes available
232 static void
233 handle_1591(struct bregs *regs)
234 {
235 }
236
237 // keyboard intercept
238 static void
239 handle_154f(struct bregs *regs)
240 {
241     set_invalid_silent(regs);
242 }
243
244 static void
245 handle_15c0(struct bregs *regs)
246 {
247     regs->es = SEG_BIOS;
248     regs->bx = (u32)&BIOS_CONFIG_TABLE;
249     set_code_success(regs);
250 }
251
252 static void
253 handle_15c1(struct bregs *regs)
254 {
255     regs->es = get_ebda_seg();
256     set_success(regs);
257 }
258
259 static void
260 handle_15e801(struct bregs *regs)
261 {
262     // my real system sets ax and bx to 0
263     // this is confirmed by Ralph Brown list
264     // but syslinux v1.48 is known to behave
265     // strangely if ax is set to 0
266     // regs.u.r16.ax = 0;
267     // regs.u.r16.bx = 0;
268
269     u32 rs = GET_GLOBAL(LegacyRamSize);
270
271     // Get the amount of extended memory (above 1M)
272     if (rs > 16*1024*1024) {
273         // limit to 15M
274         regs->cx = 15*1024;
275         // Get the amount of extended memory above 16M in 64k blocks
276         regs->dx = (rs - 16*1024*1024) / (64*1024);
277     } else {
278         regs->cx = (rs - 1*1024*1024) / 1024;
279         regs->dx = 0;
280     }
281
282     // Set configured memory equal to extended memory
283     regs->ax = regs->cx;
284     regs->bx = regs->dx;
285
286     set_success(regs);
287 }
288
289 static void
290 handle_15e820(struct bregs *regs)
291 {
292     int count = GET_GLOBAL(e820_count);
293     if (regs->edx != 0x534D4150 || regs->bx >= count
294         || regs->ecx < sizeof(e820_list[0])) {
295         set_code_invalid(regs, RET_EUNSUPPORTED);
296         return;
297     }
298
299     memcpy_far(regs->es, (void*)(regs->di+0)
300                , get_global_seg(), &e820_list[regs->bx]
301                , sizeof(e820_list[0]));
302     if (regs->bx == count-1)
303         regs->ebx = 0;
304     else
305         regs->ebx++;
306     regs->eax = 0x534D4150;
307     regs->ecx = sizeof(e820_list[0]);
308     set_success(regs);
309 }
310
311 static void
312 handle_15e8XX(struct bregs *regs)
313 {
314     set_code_unimplemented(regs, RET_EUNSUPPORTED);
315 }
316
317 static void
318 handle_15e8(struct bregs *regs)
319 {
320     switch (regs->al) {
321     case 0x01: handle_15e801(regs); break;
322     case 0x20: handle_15e820(regs); break;
323     default:   handle_15e8XX(regs); break;
324     }
325 }
326
327 static void
328 handle_15XX(struct bregs *regs)
329 {
330     set_code_unimplemented(regs, RET_EUNSUPPORTED);
331 }
332
333 // INT 15h System Services Entry Point
334 void VISIBLE16
335 handle_15(struct bregs *regs)
336 {
337     debug_enter(regs, DEBUG_HDL_15);
338     switch (regs->ah) {
339     case 0x24: handle_1524(regs); break;
340     case 0x4f: handle_154f(regs); break;
341     case 0x52: handle_1552(regs); break;
342     case 0x53: handle_1553(regs); break;
343     case 0x5f: handle_155f(regs); break;
344     case 0x7f: handle_157f(regs); break;
345     case 0x83: handle_1583(regs); break;
346     case 0x86: handle_1586(regs); break;
347     case 0x87: handle_1587(regs); break;
348     case 0x88: handle_1588(regs); break;
349     case 0x90: handle_1590(regs); break;
350     case 0x91: handle_1591(regs); break;
351     case 0xc0: handle_15c0(regs); break;
352     case 0xc1: handle_15c1(regs); break;
353     case 0xc2: handle_15c2(regs); break;
354     case 0xe8: handle_15e8(regs); break;
355     default:   handle_15XX(regs); break;
356     }
357 }