blob: 0941565976ebe5dca485b3b2970b8c2263334a1c [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
Kevin O'Connor2ad37442008-05-06 19:49:01 -04009#include "biosvar.h" // BIOS_CONFIG_TABLE
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050010#include "ioport.h" // inb
Kevin O'Connorc7812932008-06-08 23:08:12 -040011#include "memmap.h" // E820_RAM
Kevin O'Connore2e5f012008-03-08 10:27:39 -050012
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);
Kevin O'Connor6c781222008-03-09 12:19:23 -040032 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050033}
34
35static void
36handle_152401(struct bregs *regs)
37{
38 set_a20(1);
Kevin O'Connor6c781222008-03-09 12:19:23 -040039 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050040}
41
42static void
43handle_152402(struct bregs *regs)
44{
45 regs->al = !!(inb(PORT_A20) & 0x20);
Kevin O'Connor6c781222008-03-09 12:19:23 -040046 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050047}
48
49static void
50handle_152403(struct bregs *regs)
51{
52 regs->bx = 3;
Kevin O'Connor6c781222008-03-09 12:19:23 -040053 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050054}
55
56static void
57handle_1524XX(struct bregs *regs)
58{
Kevin O'Connor6c781222008-03-09 12:19:23 -040059 set_code_fail(regs, RET_EUNSUPPORTED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050060}
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{
Kevin O'Connor6c781222008-03-09 12:19:23 -040078 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050079}
80
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050081static void
82handle_1587(struct bregs *regs)
83{
84 // +++ should probably have descriptor checks
85 // +++ should have exception handlers
86
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050087 u8 prev_a20_enable = set_a20(1); // enable A20 line
88
89 // 128K max of transfer on 386+ ???
90 // source == destination ???
91
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
101
102 //es:si
103 //eeee0
104 //0ssss
105 //-----
106
107// check for access rights of source & dest here
108
109 // Initialize GDT descriptor
Kevin O'Connor67c00dd2008-03-09 16:10:19 -0400110 SET_SEG(ES, regs->es);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500111 u16 si = regs->si;
112 u16 base15_00 = (regs->es << 4) + si;
113 u16 base23_16 = regs->es >> 12;
Kevin O'Connor67c00dd2008-03-09 16:10:19 -0400114 if (base15_00 < (u16)(regs->es<<4))
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500115 base23_16++;
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
121
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
128
129 // Initialize SS descriptor
130 u16 ss = GET_SEG(SS);
131 base15_00 = ss << 4;
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
138
Kevin O'Connor2cdd8b62008-03-09 23:37:04 -0400139 u16 count = regs->cx;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500140 asm volatile(
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500141 // Load new descriptor tables
Kevin O'Connor67c00dd2008-03-09 16:10:19 -0400142 "lgdtw %%es:0x8(%%si)\n"
143 "lidtw %%cs:pmode_IDT_info\n"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500144
145 // set PE bit in CR0
146 "movl %%cr0, %%eax\n"
147 "orb $0x01, %%al\n"
148 "movl %%eax, %%cr0\n"
149
150 // far jump to flush CPU queue after transition to protected mode
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500151 "ljmpw $0x0020, $1f\n"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500152 "1:\n"
153
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
156 "movw %%ax, %%ds\n"
157 "movw $0x18, %%ax\n" // 011 000 = 3rd descriptor in table, TI=GDT, RPL=00
158 "movw %%ax, %%es\n"
159
160 // move CX words from DS:SI to ES:DI
161 "xorw %%si, %%si\n"
162 "xorw %%di, %%di\n"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500163 "rep movsw\n"
164
165 // reset PG bit in CR0 ???
166 "movl %%cr0, %%eax\n"
167 "andb $0xfe, %%al\n"
168 "movl %%eax, %%cr0\n"
169
170 // far jump to flush CPU queue after transition to real mode
171 "ljmpw $0xf000, $2f\n"
172 "2:\n"
173
174 // restore IDT to normal real-mode defaults
175 "lidt %%cs:rmode_IDT_info\n"
176
Kevin O'Connore20ed9f2008-03-01 14:25:44 -0500177 // Restore %ds (from %ss)
178 "movw %%ss, %%ax\n"
179 "movw %%ax, %%ds\n"
Kevin O'Connor2cdd8b62008-03-09 23:37:04 -0400180 : "+c"(count), "+S"(si)
Kevin O'Connored128492008-03-11 11:14:59 -0400181 : : "eax", "di", "cc"); // XXX - also clobbers %es
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500182
183 set_a20(prev_a20_enable);
184
Kevin O'Connor6c781222008-03-09 12:19:23 -0400185 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500186}
187
188// Get the amount of extended memory (above 1M)
189static void
190handle_1588(struct bregs *regs)
191{
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400192 u32 rs = GET_EBDA(ram_size);
193
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500194 // According to Ralf Brown's interrupt the limit should be 15M,
195 // but real machines mostly return max. 63M.
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400196 if (rs > 64*1024*1024)
197 regs->ax = 63 * 1024;
198 else
199 regs->ax = (rs - 1*1024*1024) / 1024;
Kevin O'Connor6c781222008-03-09 12:19:23 -0400200 set_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500201}
202
203// Device busy interrupt. Called by Int 16h when no key available
204static void
205handle_1590(struct bregs *regs)
206{
207}
208
209// Interrupt complete. Called by Int 16h when key becomes available
210static void
211handle_1591(struct bregs *regs)
212{
213}
214
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500215// keyboard intercept
216static void
217handle_154f(struct bregs *regs)
218{
Kevin O'Connordb9e65e2008-06-07 14:41:21 -0400219 set_fail_silent(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500220}
221
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500222static void
223handle_15c0(struct bregs *regs)
224{
225 regs->es = SEG_BIOS;
Kevin O'Connor117fc212008-04-13 18:17:02 -0400226 regs->bx = (u32)&BIOS_CONFIG_TABLE;
Kevin O'Connor6c781222008-03-09 12:19:23 -0400227 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500228}
229
230static void
231handle_15c1(struct bregs *regs)
232{
233 regs->es = GET_BDA(ebda_seg);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400234 set_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500235}
236
237static void
238handle_15e801(struct bregs *regs)
239{
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;
246
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400247 u32 rs = GET_EBDA(ram_size);
248
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500249 // Get the amount of extended memory (above 1M)
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400250 if (rs > 16*1024*1024) {
251 // limit to 15M
252 regs->cx = 15*1024;
253 // Get the amount of extended memory above 16M in 64k blocks
254 regs->dx = (rs - 16*1024*1024) / (64*1024);
255 } else {
256 regs->cx = (rs - 1*1024*1024) / 1024;
257 regs->dx = 0;
258 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500259
260 // Set configured memory equal to extended memory
261 regs->ax = regs->cx;
262 regs->bx = regs->dx;
263
Kevin O'Connor6c781222008-03-09 12:19:23 -0400264 set_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500265}
266
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500267static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500268handle_15e820(struct bregs *regs)
269{
Kevin O'Connorc7812932008-06-08 23:08:12 -0400270 int count = GET_EBDA(e820_count);
271 if (regs->edx != 0x534D4150 || regs->bx >= count) {
Kevin O'Connor6c781222008-03-09 12:19:23 -0400272 set_code_fail(regs, RET_EUNSUPPORTED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500273 return;
274 }
275
Kevin O'Connorc7812932008-06-08 23:08:12 -0400276 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)
279 regs->ebx = 0;
280 else
281 regs->ebx++;
282 regs->eax = 0x534D4150;
283 regs->ecx = sizeof(*e);
284 set_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500285}
286
287static void
288handle_15e8XX(struct bregs *regs)
289{
Kevin O'Connor6c781222008-03-09 12:19:23 -0400290 set_code_fail(regs, RET_EUNSUPPORTED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500291}
292
293static void
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500294handle_15e8(struct bregs *regs)
295{
296 switch (regs->al) {
297 case 0x01: handle_15e801(regs); break;
298 case 0x20: handle_15e820(regs); break;
299 default: handle_15e8XX(regs); break;
300 }
301}
302
303static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500304handle_15XX(struct bregs *regs)
305{
Kevin O'Connor6c781222008-03-09 12:19:23 -0400306 set_code_fail(regs, RET_EUNSUPPORTED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500307}
308
309// INT 15h System Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500310void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500311handle_15(struct bregs *regs)
312{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400313 debug_enter(regs, DEBUG_HDL_15);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500314 switch (regs->ah) {
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500315 case 0x24: handle_1524(regs); break;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500316 case 0x4f: handle_154f(regs); break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500317 case 0x52: handle_1552(regs); break;
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500318 case 0x53: handle_1553(regs); break;
319 case 0x83: handle_1583(regs); break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500320 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;
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500328 case 0xe8: handle_15e8(regs); break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500329 default: handle_15XX(regs); break;
330 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500331}
332
333// INT 12h Memory Size Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500334void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500335handle_12(struct bregs *regs)
336{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400337 debug_enter(regs, DEBUG_HDL_12);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500338 regs->ax = GET_BDA(mem_size_kb);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500339}
340
341// INT 11h Equipment List Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500342void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500343handle_11(struct bregs *regs)
344{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400345 debug_enter(regs, DEBUG_HDL_11);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500346 regs->ax = GET_BDA(equipment_list_flags);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500347}
348
349// INT 05h Print Screen Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500350void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500351handle_05(struct bregs *regs)
352{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400353 debug_enter(regs, DEBUG_HDL_05);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500354}
355
356// INT 10h Video Support Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500357void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500358handle_10(struct bregs *regs)
359{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400360 debug_enter(regs, DEBUG_HDL_10);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500361 // dont do anything, since the VGA BIOS handles int10h requests
362}
363
Kevin O'Connor19786762008-03-05 21:09:59 -0500364void VISIBLE16
Kevin O'Connored128492008-03-11 11:14:59 -0400365handle_nmi()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500366{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400367 debug_isr(DEBUG_ISR_nmi);
Kevin O'Connored128492008-03-11 11:14:59 -0400368 BX_PANIC("NMI Handler called\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500369}
370
371// INT 75 - IRQ13 - MATH COPROCESSOR EXCEPTION
Kevin O'Connor19786762008-03-05 21:09:59 -0500372void VISIBLE16
Kevin O'Connored128492008-03-11 11:14:59 -0400373handle_75()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500374{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400375 debug_isr(DEBUG_ISR_75);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500376
377 // clear irq13
378 outb(0, PORT_MATH_CLEAR);
379 // clear interrupt
380 eoi_both_pics();
381 // legacy nmi call
382 struct bregs br;
383 memset(&br, 0, sizeof(br));
384 call16_int(0x02, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500385}