blob: f3b0d0891e1faeec9477c1541475de79195e1cb1 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 16bit system callbacks
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 GPLv3 license.
7
8#include "util.h" // irq_restore
9#include "biosvar.h" // CONFIG_BIOS_TABLE
10#include "ioport.h" // inb
11#include "cmos.h" // inb_cmos
12
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050013// Use PS2 System Control port A to set A20 enable
14static inline u8
15set_a20(u8 cond)
16{
17 // get current setting first
18 u8 newval, oldval = inb(PORT_A20);
19 if (cond)
20 newval = oldval | 0x02;
21 else
22 newval = oldval & ~0x02;
23 outb(newval, PORT_A20);
24
25 return (newval & 0x02) != 0;
26}
27
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050028static void
29handle_152400(struct bregs *regs)
30{
31 set_a20(0);
32 handle_ret(regs, 0);
33}
34
35static void
36handle_152401(struct bregs *regs)
37{
38 set_a20(1);
39 handle_ret(regs, 0);
40}
41
42static void
43handle_152402(struct bregs *regs)
44{
45 regs->al = !!(inb(PORT_A20) & 0x20);
46 handle_ret(regs, 0);
47}
48
49static void
50handle_152403(struct bregs *regs)
51{
52 regs->bx = 3;
53 handle_ret(regs, 0);
54}
55
56static void
57handle_1524XX(struct bregs *regs)
58{
59 handle_ret(regs, RET_EUNSUPPORTED);
60}
61
Kevin O'Connoradb6b372008-03-01 13:38:38 -050062static void
63handle_1524(struct bregs *regs)
64{
65 switch (regs->al) {
66 case 0x00: handle_152400(regs); break;
67 case 0x01: handle_152401(regs); break;
68 case 0x02: handle_152402(regs); break;
69 case 0x03: handle_152403(regs); break;
70 default: handle_1524XX(regs); break;
71 }
72}
73
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050074// removable media eject
75static void
76handle_1552(struct bregs *regs)
77{
78 handle_ret(regs, 0);
79}
80
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050081static void
Kevin O'Connorbdce35f2008-02-26 21:33:14 -050082handle_1553(struct bregs *regs)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050083{
Kevin O'Connorbdce35f2008-02-26 21:33:14 -050084 // XXX - APM call
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050085 handle_ret(regs, RET_EUNSUPPORTED);
86}
87
88// Sleep for n microseconds. currently using the
89// refresh request port 0x61 bit4, toggling every 15usec
90static void
91usleep(u32 count)
92{
93 count = count / 15;
94 u8 kbd = inb(PORT_KBD_CTRLB);
95 while (count)
96 if ((inb(PORT_KBD_CTRLB) ^ kbd) & KBD_REFRESH)
97 count--;
98}
99
100// Wait for CX:DX microseconds. currently using the
101// refresh request port 0x61 bit4, toggling every 15usec
102static void
103handle_1586(struct bregs *regs)
104{
105 irq_enable();
106 usleep((regs->cx << 16) | regs->dx);
107 irq_disable();
108}
109
110static void
111handle_1587(struct bregs *regs)
112{
113 // +++ should probably have descriptor checks
114 // +++ should have exception handlers
115
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500116 u8 prev_a20_enable = set_a20(1); // enable A20 line
117
118 // 128K max of transfer on 386+ ???
119 // source == destination ???
120
121 // ES:SI points to descriptor table
122 // offset use initially comments
123 // ==============================================
124 // 00..07 Unused zeros Null descriptor
125 // 08..0f GDT zeros filled in by BIOS
126 // 10..17 source ssssssss source of data
127 // 18..1f dest dddddddd destination of data
128 // 20..27 CS zeros filled in by BIOS
129 // 28..2f SS zeros filled in by BIOS
130
131 //es:si
132 //eeee0
133 //0ssss
134 //-----
135
136// check for access rights of source & dest here
137
138 // Initialize GDT descriptor
139 u16 si = regs->si;
140 u16 base15_00 = (regs->es << 4) + si;
141 u16 base23_16 = regs->es >> 12;
142 if (base15_00 < (regs->es<<4))
143 base23_16++;
144 SET_VAR(ES, *(u16*)(si+0x08+0), 47); // limit 15:00 = 6 * 8bytes/descriptor
145 SET_VAR(ES, *(u16*)(si+0x08+2), base15_00);// base 15:00
146 SET_VAR(ES, *(u8 *)(si+0x08+4), base23_16);// base 23:16
147 SET_VAR(ES, *(u8 *)(si+0x08+5), 0x93); // access
148 SET_VAR(ES, *(u16*)(si+0x08+6), 0x0000); // base 31:24/reserved/limit 19:16
149
150 // Initialize CS descriptor
151 SET_VAR(ES, *(u16*)(si+0x20+0), 0xffff);// limit 15:00 = normal 64K limit
152 SET_VAR(ES, *(u16*)(si+0x20+2), 0x0000);// base 15:00
153 SET_VAR(ES, *(u8 *)(si+0x20+4), 0x000f);// base 23:16
154 SET_VAR(ES, *(u8 *)(si+0x20+5), 0x9b); // access
155 SET_VAR(ES, *(u16*)(si+0x20+6), 0x0000);// base 31:24/reserved/limit 19:16
156
157 // Initialize SS descriptor
158 u16 ss = GET_SEG(SS);
159 base15_00 = ss << 4;
160 base23_16 = ss >> 12;
161 SET_VAR(ES, *(u16*)(si+0x28+0), 0xffff); // limit 15:00 = normal 64K limit
162 SET_VAR(ES, *(u16*)(si+0x28+2), base15_00);// base 15:00
163 SET_VAR(ES, *(u8 *)(si+0x28+4), base23_16);// base 23:16
164 SET_VAR(ES, *(u8 *)(si+0x28+5), 0x93); // access
165 SET_VAR(ES, *(u16*)(si+0x28+6), 0x0000); // base 31:24/reserved/limit 19:16
166
167 asm volatile(
168 // Save registers
169 "pushw %%ds\n"
170 "pushw %%es\n"
171 "pushal\n"
172
173 // Load new descriptor tables
174 "lgdt %%es:(%1)\n"
175 "lidt %%cs:pmode_IDT_info\n"
176
177 // set PE bit in CR0
178 "movl %%cr0, %%eax\n"
179 "orb $0x01, %%al\n"
180 "movl %%eax, %%cr0\n"
181
182 // far jump to flush CPU queue after transition to protected mode
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500183 "ljmpw $0x0020, $1f\n"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500184 "1:\n"
185
186 // GDT points to valid descriptor table, now load DS, ES
187 "movw $0x10, %%ax\n" // 010 000 = 2nd descriptor in table, TI=GDT, RPL=00
188 "movw %%ax, %%ds\n"
189 "movw $0x18, %%ax\n" // 011 000 = 3rd descriptor in table, TI=GDT, RPL=00
190 "movw %%ax, %%es\n"
191
192 // move CX words from DS:SI to ES:DI
193 "xorw %%si, %%si\n"
194 "xorw %%di, %%di\n"
195 "cld\n"
196 "rep movsw\n"
197
198 // reset PG bit in CR0 ???
199 "movl %%cr0, %%eax\n"
200 "andb $0xfe, %%al\n"
201 "movl %%eax, %%cr0\n"
202
203 // far jump to flush CPU queue after transition to real mode
204 "ljmpw $0xf000, $2f\n"
205 "2:\n"
206
207 // restore IDT to normal real-mode defaults
208 "lidt %%cs:rmode_IDT_info\n"
209
210 // restore regisers
211 "popal\n"
212 "popw %%es\n"
213 "popw %%ds\n" : : "c" (regs->cx), "r" (si + 8));
214
215 set_a20(prev_a20_enable);
216
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500217 handle_ret(regs, 0);
218}
219
220// Get the amount of extended memory (above 1M)
221static void
222handle_1588(struct bregs *regs)
223{
224 regs->al = inb_cmos(CMOS_EXTMEM_LOW);
225 regs->ah = inb_cmos(CMOS_EXTMEM_HIGH);
226 // According to Ralf Brown's interrupt the limit should be 15M,
227 // but real machines mostly return max. 63M.
228 if (regs->ax > 0xffc0)
229 regs->ax = 0xffc0;
230 set_cf(regs, 0);
231}
232
233// Device busy interrupt. Called by Int 16h when no key available
234static void
235handle_1590(struct bregs *regs)
236{
237}
238
239// Interrupt complete. Called by Int 16h when key becomes available
240static void
241handle_1591(struct bregs *regs)
242{
243}
244
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500245// keyboard intercept
246static void
247handle_154f(struct bregs *regs)
248{
249 set_cf(regs, 1);
250}
251
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500252static void
253handle_15c0(struct bregs *regs)
254{
255 regs->es = SEG_BIOS;
256 regs->bx = (u16)&BIOS_CONFIG_TABLE;
257}
258
259static void
260handle_15c1(struct bregs *regs)
261{
262 regs->es = GET_BDA(ebda_seg);
263 set_cf(regs, 0);
264}
265
266static void
267handle_15e801(struct bregs *regs)
268{
269 // my real system sets ax and bx to 0
270 // this is confirmed by Ralph Brown list
271 // but syslinux v1.48 is known to behave
272 // strangely if ax is set to 0
273 // regs.u.r16.ax = 0;
274 // regs.u.r16.bx = 0;
275
276 // Get the amount of extended memory (above 1M)
277 regs->cl = inb_cmos(CMOS_EXTMEM_LOW);
278 regs->ch = inb_cmos(CMOS_EXTMEM_HIGH);
279
280 // limit to 15M
281 if (regs->cx > 0x3c00)
282 regs->cx = 0x3c00;
283
284 // Get the amount of extended memory above 16M in 64k blocs
285 regs->dl = inb_cmos(CMOS_EXTMEM2_LOW);
286 regs->dh = inb_cmos(CMOS_EXTMEM2_HIGH);
287
288 // Set configured memory equal to extended memory
289 regs->ax = regs->cx;
290 regs->bx = regs->dx;
291
292 set_cf(regs, 0);
293}
294
295#define ACPI_DATA_SIZE 0x00010000L
296
297static void
298set_e820_range(u16 DI, u32 start, u32 end, u16 type)
299{
300 SET_VAR(ES, *(u16*)(DI+0), start);
301 SET_VAR(ES, *(u16*)(DI+2), start >> 16);
302 SET_VAR(ES, *(u16*)(DI+4), 0x00);
303 SET_VAR(ES, *(u16*)(DI+6), 0x00);
304
305 end -= start;
306 SET_VAR(ES, *(u16*)(DI+8), end);
307 SET_VAR(ES, *(u16*)(DI+10), end >> 16);
308 SET_VAR(ES, *(u16*)(DI+12), 0x0000);
309 SET_VAR(ES, *(u16*)(DI+14), 0x0000);
310
311 SET_VAR(ES, *(u16*)(DI+16), type);
312 SET_VAR(ES, *(u16*)(DI+18), 0x0);
313}
314
315// XXX - should create e820 memory map in post and just copy it here.
316static void
317handle_15e820(struct bregs *regs)
318{
319 if (regs->edx != 0x534D4150) {
320 handle_ret(regs, RET_EUNSUPPORTED);
321 return;
322 }
323
324 u32 extended_memory_size = inb_cmos(CMOS_EXTMEM2_HIGH);
325 extended_memory_size <<= 8;
326 extended_memory_size |= inb_cmos(CMOS_EXTMEM2_LOW);
327 extended_memory_size *= 64;
328 // greater than EFF00000???
329 if (extended_memory_size > 0x3bc000)
330 // everything after this is reserved memory until we get to 0x100000000
331 extended_memory_size = 0x3bc000;
332 extended_memory_size *= 1024;
333 extended_memory_size += (16L * 1024 * 1024);
334
335 if (extended_memory_size <= (16L * 1024 * 1024)) {
336 extended_memory_size = inb_cmos(CMOS_EXTMEM_HIGH);
337 extended_memory_size <<= 8;
338 extended_memory_size |= inb_cmos(CMOS_EXTMEM_LOW);
339 extended_memory_size *= 1024;
340 }
341
342 switch (regs->bx) {
343 case 0:
344 set_e820_range(regs->di, 0x0000000L, 0x0009fc00L, 1);
345 regs->ebx = 1;
346 regs->eax = 0x534D4150;
347 regs->ecx = 0x14;
348 set_cf(regs, 0);
349 break;
350 case 1:
351 set_e820_range(regs->di, 0x0009fc00L, 0x000a0000L, 2);
352 regs->ebx = 2;
353 regs->eax = 0x534D4150;
354 regs->ecx = 0x14;
355 set_cf(regs, 0);
356 break;
357 case 2:
358 set_e820_range(regs->di, 0x000e8000L, 0x00100000L, 2);
359 regs->ebx = 3;
360 regs->eax = 0x534D4150;
361 regs->ecx = 0x14;
362 set_cf(regs, 0);
363 break;
364 case 3:
365 set_e820_range(regs->di, 0x00100000L,
366 extended_memory_size - ACPI_DATA_SIZE, 1);
367 regs->ebx = 4;
368 regs->eax = 0x534D4150;
369 regs->ecx = 0x14;
370 set_cf(regs, 0);
371 break;
372 case 4:
373 set_e820_range(regs->di,
374 extended_memory_size - ACPI_DATA_SIZE,
375 extended_memory_size, 3); // ACPI RAM
376 regs->ebx = 5;
377 regs->eax = 0x534D4150;
378 regs->ecx = 0x14;
379 set_cf(regs, 0);
380 break;
381 case 5:
382 /* 256KB BIOS area at the end of 4 GB */
383 set_e820_range(regs->di, 0xfffc0000L, 0x00000000L, 2);
384 regs->ebx = 0;
385 regs->eax = 0x534D4150;
386 regs->ecx = 0x14;
387 set_cf(regs, 0);
388 break;
389 default: /* AX=E820, DX=534D4150, BX unrecognized */
390 handle_ret(regs, RET_EUNSUPPORTED);
391 }
392}
393
394static void
395handle_15e8XX(struct bregs *regs)
396{
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500397 handle_ret(regs, RET_EUNSUPPORTED);
398}
399
400static void
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500401handle_15e8(struct bregs *regs)
402{
403 switch (regs->al) {
404 case 0x01: handle_15e801(regs); break;
405 case 0x20: handle_15e820(regs); break;
406 default: handle_15e8XX(regs); break;
407 }
408}
409
410static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500411handle_15XX(struct bregs *regs)
412{
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500413 handle_ret(regs, RET_EUNSUPPORTED);
414}
415
416// INT 15h System Services Entry Point
417void VISIBLE
418handle_15(struct bregs *regs)
419{
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500420 //debug_enter(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500421 switch (regs->ah) {
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500422 case 0x24: handle_1524(regs); break;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500423 case 0x4f: handle_154f(regs); break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500424 case 0x52: handle_1552(regs); break;
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500425 case 0x53: handle_1553(regs); break;
426 case 0x83: handle_1583(regs); break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500427 case 0x86: handle_1586(regs); break;
428 case 0x87: handle_1587(regs); break;
429 case 0x88: handle_1588(regs); break;
430 case 0x90: handle_1590(regs); break;
431 case 0x91: handle_1591(regs); break;
432 case 0xc0: handle_15c0(regs); break;
433 case 0xc1: handle_15c1(regs); break;
434 case 0xc2: handle_15c2(regs); break;
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500435 case 0xe8: handle_15e8(regs); break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500436 default: handle_15XX(regs); break;
437 }
438 debug_exit(regs);
439}
440
441// INT 12h Memory Size Service Entry Point
442void VISIBLE
443handle_12(struct bregs *regs)
444{
445 debug_enter(regs);
446 regs->ax = GET_BDA(mem_size_kb);
447 debug_exit(regs);
448}
449
450// INT 11h Equipment List Service Entry Point
451void VISIBLE
452handle_11(struct bregs *regs)
453{
454 debug_enter(regs);
455 regs->ax = GET_BDA(equipment_list_flags);
456 debug_exit(regs);
457}
458
459// INT 05h Print Screen Service Entry Point
460void VISIBLE
461handle_05(struct bregs *regs)
462{
463 debug_enter(regs);
464}
465
466// INT 10h Video Support Service Entry Point
467void VISIBLE
468handle_10(struct bregs *regs)
469{
470 debug_enter(regs);
471 // dont do anything, since the VGA BIOS handles int10h requests
472}
473
474void VISIBLE
475handle_nmi(struct bregs *regs)
476{
477 debug_enter(regs);
478 // XXX
479}
480
481// INT 75 - IRQ13 - MATH COPROCESSOR EXCEPTION
482void VISIBLE
483handle_75(struct bregs *regs)
484{
485 debug_enter(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500486
487 // clear irq13
488 outb(0, PORT_MATH_CLEAR);
489 // clear interrupt
490 eoi_both_pics();
491 // legacy nmi call
492 struct bregs br;
493 memset(&br, 0, sizeof(br));
494 call16_int(0x02, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500495}