1 // 16bit system callbacks
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU GPLv3 license.
8 #include "util.h" // irq_restore
9 #include "biosvar.h" // CONFIG_BIOS_TABLE
10 #include "ioport.h" // inb
11 #include "cmos.h" // inb_cmos
13 #define RET_EUNSUPPORTED 0x86
16 // Use PS2 System Control port A to set A20 enable
20 // get current setting first
21 u8 newval, oldval = inb(PORT_A20);
23 newval = oldval | 0x02;
25 newval = oldval & ~0x02;
26 outb(newval, PORT_A20);
28 return (newval & 0x02) != 0;
32 handle_ret(struct bregs *regs, u8 code)
39 handle_152400(struct bregs *regs)
46 handle_152401(struct bregs *regs)
53 handle_152402(struct bregs *regs)
55 regs->al = !!(inb(PORT_A20) & 0x20);
60 handle_152403(struct bregs *regs)
67 handle_1524XX(struct bregs *regs)
69 handle_ret(regs, RET_EUNSUPPORTED);
72 // removable media eject
74 handle_1552(struct bregs *regs)
79 // Set Interval requested.
81 handle_158300(struct bregs *regs)
83 if (GET_BDA(rtc_wait_flag) & RWS_WAIT_PENDING) {
84 // Interval already set.
85 DEBUGF("int15: Func 83h, failed, already waiting.\n" );
86 handle_ret(regs, RET_EUNSUPPORTED);
88 // Interval not already set.
89 SET_BDA(rtc_wait_flag, RWS_WAIT_PENDING); // Set status byte.
90 u32 v = (regs->es << 16) | regs->bx;
91 SET_BDA(ptr_user_wait_complete_flag, v);
92 v = (regs->dx << 16) | regs->cx;
93 SET_BDA(user_wait_timeout, v);
95 // Unmask IRQ8 so INT70 will get through.
96 u8 irqDisable = inb(PORT_PIC2_DATA);
97 outb(irqDisable & ~PIC2_IRQ8, PORT_PIC2_DATA);
98 // Turn on the Periodic Interrupt timer
99 u8 bRegister = inb_cmos(CMOS_STATUS_B);
100 outb_cmos(CMOS_STATUS_B, bRegister | CSB_EN_ALARM_IRQ);
102 set_cf(regs, 0); // XXX - no set ah?
105 // Clear interval requested
107 handle_158301(struct bregs *regs)
109 SET_BDA(rtc_wait_flag, 0); // Clear status byte
110 // Turn off the Periodic Interrupt timer
111 u8 bRegister = inb_cmos(CMOS_STATUS_B);
112 outb_cmos(CMOS_STATUS_B, bRegister & ~CSB_EN_ALARM_IRQ);
113 set_cf(regs, 0); // XXX - no set ah?
117 handle_1583XX(struct bregs *regs)
120 handle_ret(regs, RET_EUNSUPPORTED);
123 // Sleep for n microseconds. currently using the
124 // refresh request port 0x61 bit4, toggling every 15usec
129 u8 kbd = inb(PORT_KBD_CTRLB);
131 if ((inb(PORT_KBD_CTRLB) ^ kbd) & KBD_REFRESH)
135 // Wait for CX:DX microseconds. currently using the
136 // refresh request port 0x61 bit4, toggling every 15usec
138 handle_1586(struct bregs *regs)
141 usleep((regs->cx << 16) | regs->dx);
146 handle_1587(struct bregs *regs)
148 // +++ should probably have descriptor checks
149 // +++ should have exception handlers
151 // turn off interrupts
152 unsigned long flags = irq_save();
154 u8 prev_a20_enable = set_a20(1); // enable A20 line
156 // 128K max of transfer on 386+ ???
157 // source == destination ???
159 // ES:SI points to descriptor table
160 // offset use initially comments
161 // ==============================================
162 // 00..07 Unused zeros Null descriptor
163 // 08..0f GDT zeros filled in by BIOS
164 // 10..17 source ssssssss source of data
165 // 18..1f dest dddddddd destination of data
166 // 20..27 CS zeros filled in by BIOS
167 // 28..2f SS zeros filled in by BIOS
174 // check for access rights of source & dest here
176 // Initialize GDT descriptor
178 u16 base15_00 = (regs->es << 4) + si;
179 u16 base23_16 = regs->es >> 12;
180 if (base15_00 < (regs->es<<4))
182 SET_VAR(ES, *(u16*)(si+0x08+0), 47); // limit 15:00 = 6 * 8bytes/descriptor
183 SET_VAR(ES, *(u16*)(si+0x08+2), base15_00);// base 15:00
184 SET_VAR(ES, *(u8 *)(si+0x08+4), base23_16);// base 23:16
185 SET_VAR(ES, *(u8 *)(si+0x08+5), 0x93); // access
186 SET_VAR(ES, *(u16*)(si+0x08+6), 0x0000); // base 31:24/reserved/limit 19:16
188 // Initialize CS descriptor
189 SET_VAR(ES, *(u16*)(si+0x20+0), 0xffff);// limit 15:00 = normal 64K limit
190 SET_VAR(ES, *(u16*)(si+0x20+2), 0x0000);// base 15:00
191 SET_VAR(ES, *(u8 *)(si+0x20+4), 0x000f);// base 23:16
192 SET_VAR(ES, *(u8 *)(si+0x20+5), 0x9b); // access
193 SET_VAR(ES, *(u16*)(si+0x20+6), 0x0000);// base 31:24/reserved/limit 19:16
195 // Initialize SS descriptor
196 u16 ss = GET_SEG(SS);
198 base23_16 = ss >> 12;
199 SET_VAR(ES, *(u16*)(si+0x28+0), 0xffff); // limit 15:00 = normal 64K limit
200 SET_VAR(ES, *(u16*)(si+0x28+2), base15_00);// base 15:00
201 SET_VAR(ES, *(u8 *)(si+0x28+4), base23_16);// base 23:16
202 SET_VAR(ES, *(u8 *)(si+0x28+5), 0x93); // access
203 SET_VAR(ES, *(u16*)(si+0x28+6), 0x0000); // base 31:24/reserved/limit 19:16
211 // Load new descriptor tables
213 "lidt %%cs:pmode_IDT_info\n"
216 "movl %%cr0, %%eax\n"
218 "movl %%eax, %%cr0\n"
220 // far jump to flush CPU queue after transition to protected mode
221 "ljmpw $0xf000, $1f\n"
224 // GDT points to valid descriptor table, now load DS, ES
225 "movw $0x10, %%ax\n" // 010 000 = 2nd descriptor in table, TI=GDT, RPL=00
227 "movw $0x18, %%ax\n" // 011 000 = 3rd descriptor in table, TI=GDT, RPL=00
230 // move CX words from DS:SI to ES:DI
236 // reset PG bit in CR0 ???
237 "movl %%cr0, %%eax\n"
239 "movl %%eax, %%cr0\n"
241 // far jump to flush CPU queue after transition to real mode
242 "ljmpw $0xf000, $2f\n"
245 // restore IDT to normal real-mode defaults
246 "lidt %%cs:rmode_IDT_info\n"
251 "popw %%ds\n" : : "c" (regs->cx), "r" (si + 8));
253 set_a20(prev_a20_enable);
260 // Get the amount of extended memory (above 1M)
262 handle_1588(struct bregs *regs)
264 regs->al = inb_cmos(CMOS_EXTMEM_LOW);
265 regs->ah = inb_cmos(CMOS_EXTMEM_HIGH);
266 // According to Ralf Brown's interrupt the limit should be 15M,
267 // but real machines mostly return max. 63M.
268 if (regs->ax > 0xffc0)
273 // Device busy interrupt. Called by Int 16h when no key available
275 handle_1590(struct bregs *regs)
279 // Interrupt complete. Called by Int 16h when key becomes available
281 handle_1591(struct bregs *regs)
286 handle_15c0(struct bregs *regs)
289 regs->bx = (u16)&BIOS_CONFIG_TABLE;
293 handle_15c1(struct bregs *regs)
295 regs->es = GET_BDA(ebda_seg);
300 handle_15e801(struct bregs *regs)
302 // my real system sets ax and bx to 0
303 // this is confirmed by Ralph Brown list
304 // but syslinux v1.48 is known to behave
305 // strangely if ax is set to 0
306 // regs.u.r16.ax = 0;
307 // regs.u.r16.bx = 0;
309 // Get the amount of extended memory (above 1M)
310 regs->cl = inb_cmos(CMOS_EXTMEM_LOW);
311 regs->ch = inb_cmos(CMOS_EXTMEM_HIGH);
314 if (regs->cx > 0x3c00)
317 // Get the amount of extended memory above 16M in 64k blocs
318 regs->dl = inb_cmos(CMOS_EXTMEM2_LOW);
319 regs->dh = inb_cmos(CMOS_EXTMEM2_HIGH);
321 // Set configured memory equal to extended memory
328 #define ACPI_DATA_SIZE 0x00010000L
331 set_e820_range(u16 DI, u32 start, u32 end, u16 type)
333 SET_VAR(ES, *(u16*)(DI+0), start);
334 SET_VAR(ES, *(u16*)(DI+2), start >> 16);
335 SET_VAR(ES, *(u16*)(DI+4), 0x00);
336 SET_VAR(ES, *(u16*)(DI+6), 0x00);
339 SET_VAR(ES, *(u16*)(DI+8), end);
340 SET_VAR(ES, *(u16*)(DI+10), end >> 16);
341 SET_VAR(ES, *(u16*)(DI+12), 0x0000);
342 SET_VAR(ES, *(u16*)(DI+14), 0x0000);
344 SET_VAR(ES, *(u16*)(DI+16), type);
345 SET_VAR(ES, *(u16*)(DI+18), 0x0);
348 // XXX - should create e820 memory map in post and just copy it here.
350 handle_15e820(struct bregs *regs)
352 if (regs->edx != 0x534D4150) {
353 handle_ret(regs, RET_EUNSUPPORTED);
357 u32 extended_memory_size = inb_cmos(CMOS_EXTMEM2_HIGH);
358 extended_memory_size <<= 8;
359 extended_memory_size |= inb_cmos(CMOS_EXTMEM2_LOW);
360 extended_memory_size *= 64;
361 // greater than EFF00000???
362 if (extended_memory_size > 0x3bc000)
363 // everything after this is reserved memory until we get to 0x100000000
364 extended_memory_size = 0x3bc000;
365 extended_memory_size *= 1024;
366 extended_memory_size += (16L * 1024 * 1024);
368 if (extended_memory_size <= (16L * 1024 * 1024)) {
369 extended_memory_size = inb_cmos(CMOS_EXTMEM_HIGH);
370 extended_memory_size <<= 8;
371 extended_memory_size |= inb_cmos(CMOS_EXTMEM_LOW);
372 extended_memory_size *= 1024;
377 set_e820_range(regs->di, 0x0000000L, 0x0009fc00L, 1);
379 regs->eax = 0x534D4150;
384 set_e820_range(regs->di, 0x0009fc00L, 0x000a0000L, 2);
386 regs->eax = 0x534D4150;
391 set_e820_range(regs->di, 0x000e8000L, 0x00100000L, 2);
393 regs->eax = 0x534D4150;
398 set_e820_range(regs->di, 0x00100000L,
399 extended_memory_size - ACPI_DATA_SIZE, 1);
401 regs->eax = 0x534D4150;
406 set_e820_range(regs->di,
407 extended_memory_size - ACPI_DATA_SIZE,
408 extended_memory_size, 3); // ACPI RAM
410 regs->eax = 0x534D4150;
415 /* 256KB BIOS area at the end of 4 GB */
416 set_e820_range(regs->di, 0xfffc0000L, 0x00000000L, 2);
418 regs->eax = 0x534D4150;
422 default: /* AX=E820, DX=534D4150, BX unrecognized */
423 handle_ret(regs, RET_EUNSUPPORTED);
428 handle_15e8XX(struct bregs *regs)
431 handle_ret(regs, RET_EUNSUPPORTED);
435 handle_15XX(struct bregs *regs)
438 handle_ret(regs, RET_EUNSUPPORTED);
441 // INT 15h System Services Entry Point
443 handle_15(struct bregs *regs)
449 case 0x00: handle_152400(regs); break;
450 case 0x01: handle_152401(regs); break;
451 case 0x02: handle_152402(regs); break;
452 case 0x03: handle_152403(regs); break;
453 default: handle_1524XX(regs); break;
456 case 0x52: handle_1552(regs); break;
459 case 0x00: handle_158300(regs); break;
460 case 0x01: handle_158301(regs); break;
461 default: handle_1583XX(regs); break;
464 case 0x86: handle_1586(regs); break;
465 case 0x87: handle_1587(regs); break;
466 case 0x88: handle_1588(regs); break;
467 case 0x90: handle_1590(regs); break;
468 case 0x91: handle_1591(regs); break;
469 case 0xc0: handle_15c0(regs); break;
470 case 0xc1: handle_15c1(regs); break;
471 case 0xc2: handle_15c2(regs); break;
474 case 0x01: handle_15e801(regs); break;
475 case 0x20: handle_15e820(regs); break;
476 default: handle_15e8XX(regs); break;
479 default: handle_15XX(regs); break;
484 // INT 12h Memory Size Service Entry Point
486 handle_12(struct bregs *regs)
489 regs->ax = GET_BDA(mem_size_kb);
493 // INT 11h Equipment List Service Entry Point
495 handle_11(struct bregs *regs)
498 regs->ax = GET_BDA(equipment_list_flags);
502 // INT 05h Print Screen Service Entry Point
504 handle_05(struct bregs *regs)
509 // INT 10h Video Support Service Entry Point
511 handle_10(struct bregs *regs)
514 // dont do anything, since the VGA BIOS handles int10h requests
518 handle_nmi(struct bregs *regs)
524 // INT 75 - IRQ13 - MATH COPROCESSOR EXCEPTION
526 handle_75(struct bregs *regs)