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" // BIOS_CONFIG_TABLE
10 #include "ioport.h" // inb
11 #include "memmap.h" // E820_RAM
13 // Use PS2 System Control port A to set A20 enable
17 // get current setting first
18 u8 newval, oldval = inb(PORT_A20);
20 newval = oldval | 0x02;
22 newval = oldval & ~0x02;
23 outb(newval, PORT_A20);
25 return (newval & 0x02) != 0;
29 handle_152400(struct bregs *regs)
32 set_code_success(regs);
36 handle_152401(struct bregs *regs)
39 set_code_success(regs);
43 handle_152402(struct bregs *regs)
45 regs->al = !!(inb(PORT_A20) & 0x20);
46 set_code_success(regs);
50 handle_152403(struct bregs *regs)
53 set_code_success(regs);
57 handle_1524XX(struct bregs *regs)
59 set_code_fail(regs, RET_EUNSUPPORTED);
63 handle_1524(struct bregs *regs)
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;
74 // removable media eject
76 handle_1552(struct bregs *regs)
78 set_code_success(regs);
82 handle_1587(struct bregs *regs)
84 // +++ should probably have descriptor checks
85 // +++ should have exception handlers
87 u8 prev_a20_enable = set_a20(1); // enable A20 line
89 // 128K max of transfer on 386+ ???
90 // source == destination ???
92 // ES:SI points to descriptor table
93 // offset use initially comments
94 // ==============================================
95 // 00..07 Unused zeros Null descriptor
96 // 08..0f GDT zeros filled in by BIOS
97 // 10..17 source ssssssss source of data
98 // 18..1f dest dddddddd destination of data
99 // 20..27 CS zeros filled in by BIOS
100 // 28..2f SS zeros filled in by BIOS
107 // check for access rights of source & dest here
109 // Initialize GDT descriptor
110 SET_SEG(ES, regs->es);
112 u16 base15_00 = (regs->es << 4) + si;
113 u16 base23_16 = regs->es >> 12;
114 if (base15_00 < (u16)(regs->es<<4))
116 SET_VAR(ES, *(u16*)(si+0x08+0), 47); // limit 15:00 = 6 * 8bytes/descriptor
117 SET_VAR(ES, *(u16*)(si+0x08+2), base15_00);// base 15:00
118 SET_VAR(ES, *(u8 *)(si+0x08+4), base23_16);// base 23:16
119 SET_VAR(ES, *(u8 *)(si+0x08+5), 0x93); // access
120 SET_VAR(ES, *(u16*)(si+0x08+6), 0x0000); // base 31:24/reserved/limit 19:16
122 // Initialize CS descriptor
123 SET_VAR(ES, *(u16*)(si+0x20+0), 0xffff);// limit 15:00 = normal 64K limit
124 SET_VAR(ES, *(u16*)(si+0x20+2), 0x0000);// base 15:00
125 SET_VAR(ES, *(u8 *)(si+0x20+4), 0x000f);// base 23:16
126 SET_VAR(ES, *(u8 *)(si+0x20+5), 0x9b); // access
127 SET_VAR(ES, *(u16*)(si+0x20+6), 0x0000);// base 31:24/reserved/limit 19:16
129 // Initialize SS descriptor
130 u16 ss = GET_SEG(SS);
132 base23_16 = ss >> 12;
133 SET_VAR(ES, *(u16*)(si+0x28+0), 0xffff); // limit 15:00 = normal 64K limit
134 SET_VAR(ES, *(u16*)(si+0x28+2), base15_00);// base 15:00
135 SET_VAR(ES, *(u8 *)(si+0x28+4), base23_16);// base 23:16
136 SET_VAR(ES, *(u8 *)(si+0x28+5), 0x93); // access
137 SET_VAR(ES, *(u16*)(si+0x28+6), 0x0000); // base 31:24/reserved/limit 19:16
139 u16 count = regs->cx;
141 // Load new descriptor tables
142 "lgdtw %%es:0x8(%%si)\n"
143 "lidtw %%cs:pmode_IDT_info\n"
146 "movl %%cr0, %%eax\n"
148 "movl %%eax, %%cr0\n"
150 // far jump to flush CPU queue after transition to protected mode
151 "ljmpw $0x0020, $1f\n"
154 // GDT points to valid descriptor table, now load DS, ES
155 "movw $0x10, %%ax\n" // 010 000 = 2nd descriptor in table, TI=GDT, RPL=00
157 "movw $0x18, %%ax\n" // 011 000 = 3rd descriptor in table, TI=GDT, RPL=00
160 // move CX words from DS:SI to ES:DI
165 // reset PG bit in CR0 ???
166 "movl %%cr0, %%eax\n"
168 "movl %%eax, %%cr0\n"
170 // far jump to flush CPU queue after transition to real mode
171 "ljmpw $0xf000, $2f\n"
174 // restore IDT to normal real-mode defaults
175 "lidt %%cs:rmode_IDT_info\n"
177 // Restore %ds (from %ss)
180 : "+c"(count), "+S"(si)
181 : : "eax", "di", "cc"); // XXX - also clobbers %es
183 set_a20(prev_a20_enable);
185 set_code_success(regs);
188 // Get the amount of extended memory (above 1M)
190 handle_1588(struct bregs *regs)
192 u32 rs = GET_EBDA(ram_size);
194 // According to Ralf Brown's interrupt the limit should be 15M,
195 // but real machines mostly return max. 63M.
196 if (rs > 64*1024*1024)
197 regs->ax = 63 * 1024;
199 regs->ax = (rs - 1*1024*1024) / 1024;
203 // Device busy interrupt. Called by Int 16h when no key available
205 handle_1590(struct bregs *regs)
209 // Interrupt complete. Called by Int 16h when key becomes available
211 handle_1591(struct bregs *regs)
215 // keyboard intercept
217 handle_154f(struct bregs *regs)
219 set_fail_silent(regs);
223 handle_15c0(struct bregs *regs)
226 regs->bx = (u32)&BIOS_CONFIG_TABLE;
227 set_code_success(regs);
231 handle_15c1(struct bregs *regs)
233 regs->es = GET_BDA(ebda_seg);
238 handle_15e801(struct bregs *regs)
240 // my real system sets ax and bx to 0
241 // this is confirmed by Ralph Brown list
242 // but syslinux v1.48 is known to behave
243 // strangely if ax is set to 0
244 // regs.u.r16.ax = 0;
245 // regs.u.r16.bx = 0;
247 u32 rs = GET_EBDA(ram_size);
249 // Get the amount of extended memory (above 1M)
250 if (rs > 16*1024*1024) {
253 // Get the amount of extended memory above 16M in 64k blocks
254 regs->dx = (rs - 16*1024*1024) / (64*1024);
256 regs->cx = (rs - 1*1024*1024) / 1024;
260 // Set configured memory equal to extended memory
268 handle_15e820(struct bregs *regs)
270 int count = GET_EBDA(e820_count);
271 if (regs->edx != 0x534D4150 || regs->bx >= count) {
272 set_code_fail(regs, RET_EUNSUPPORTED);
276 struct e820entry *e = &((struct e820entry *)GET_EBDA(e820_loc))[regs->bx];
277 memcpy(MAKE_FARPTR(regs->es, regs->di), e, sizeof(*e));
278 if (regs->bx == count-1)
282 regs->eax = 0x534D4150;
283 regs->ecx = sizeof(*e);
288 handle_15e8XX(struct bregs *regs)
290 set_code_fail(regs, RET_EUNSUPPORTED);
294 handle_15e8(struct bregs *regs)
297 case 0x01: handle_15e801(regs); break;
298 case 0x20: handle_15e820(regs); break;
299 default: handle_15e8XX(regs); break;
304 handle_15XX(struct bregs *regs)
306 set_code_fail(regs, RET_EUNSUPPORTED);
309 // INT 15h System Services Entry Point
311 handle_15(struct bregs *regs)
315 case 0x24: handle_1524(regs); break;
316 case 0x4f: handle_154f(regs); break;
317 case 0x52: handle_1552(regs); break;
318 case 0x53: handle_1553(regs); break;
319 case 0x83: handle_1583(regs); break;
320 case 0x86: handle_1586(regs); break;
321 case 0x87: handle_1587(regs); break;
322 case 0x88: handle_1588(regs); break;
323 case 0x90: handle_1590(regs); break;
324 case 0x91: handle_1591(regs); break;
325 case 0xc0: handle_15c0(regs); break;
326 case 0xc1: handle_15c1(regs); break;
327 case 0xc2: handle_15c2(regs); break;
328 case 0xe8: handle_15e8(regs); break;
329 default: handle_15XX(regs); break;
333 // INT 12h Memory Size Service Entry Point
335 handle_12(struct bregs *regs)
338 regs->ax = GET_BDA(mem_size_kb);
341 // INT 11h Equipment List Service Entry Point
343 handle_11(struct bregs *regs)
346 regs->ax = GET_BDA(equipment_list_flags);
349 // INT 05h Print Screen Service Entry Point
351 handle_05(struct bregs *regs)
356 // INT 10h Video Support Service Entry Point
358 handle_10(struct bregs *regs)
361 // dont do anything, since the VGA BIOS handles int10h requests
368 BX_PANIC("NMI Handler called\n");
371 // INT 75 - IRQ13 - MATH COPROCESSOR EXCEPTION
378 outb(0, PORT_MATH_CLEAR);
383 memset(&br, 0, sizeof(br));
384 call16_int(0x02, &br);