blob: 26da94138da06ef6eb416cc83ce8269c2abd0ab8 [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'Connorf076a3e2008-02-25 22:25:15 -050011
Kevin O'Connore2e5f012008-03-08 10:27:39 -050012#define E820_RAM 1
13#define E820_RESERVED 2
14#define E820_ACPI 3
15#define E820_NVS 4
16#define E820_UNUSABLE 5
17
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050018// Use PS2 System Control port A to set A20 enable
19static inline u8
20set_a20(u8 cond)
21{
22 // get current setting first
23 u8 newval, oldval = inb(PORT_A20);
24 if (cond)
25 newval = oldval | 0x02;
26 else
27 newval = oldval & ~0x02;
28 outb(newval, PORT_A20);
29
30 return (newval & 0x02) != 0;
31}
32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050033static void
34handle_152400(struct bregs *regs)
35{
36 set_a20(0);
Kevin O'Connor6c781222008-03-09 12:19:23 -040037 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050038}
39
40static void
41handle_152401(struct bregs *regs)
42{
43 set_a20(1);
Kevin O'Connor6c781222008-03-09 12:19:23 -040044 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050045}
46
47static void
48handle_152402(struct bregs *regs)
49{
50 regs->al = !!(inb(PORT_A20) & 0x20);
Kevin O'Connor6c781222008-03-09 12:19:23 -040051 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050052}
53
54static void
55handle_152403(struct bregs *regs)
56{
57 regs->bx = 3;
Kevin O'Connor6c781222008-03-09 12:19:23 -040058 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050059}
60
61static void
62handle_1524XX(struct bregs *regs)
63{
Kevin O'Connor6c781222008-03-09 12:19:23 -040064 set_code_fail(regs, RET_EUNSUPPORTED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050065}
66
Kevin O'Connoradb6b372008-03-01 13:38:38 -050067static void
68handle_1524(struct bregs *regs)
69{
70 switch (regs->al) {
71 case 0x00: handle_152400(regs); break;
72 case 0x01: handle_152401(regs); break;
73 case 0x02: handle_152402(regs); break;
74 case 0x03: handle_152403(regs); break;
75 default: handle_1524XX(regs); break;
76 }
77}
78
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050079// removable media eject
80static void
81handle_1552(struct bregs *regs)
82{
Kevin O'Connor6c781222008-03-09 12:19:23 -040083 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050084}
85
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050086static void
87handle_1587(struct bregs *regs)
88{
89 // +++ should probably have descriptor checks
90 // +++ should have exception handlers
91
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050092 u8 prev_a20_enable = set_a20(1); // enable A20 line
93
94 // 128K max of transfer on 386+ ???
95 // source == destination ???
96
97 // ES:SI points to descriptor table
98 // offset use initially comments
99 // ==============================================
100 // 00..07 Unused zeros Null descriptor
101 // 08..0f GDT zeros filled in by BIOS
102 // 10..17 source ssssssss source of data
103 // 18..1f dest dddddddd destination of data
104 // 20..27 CS zeros filled in by BIOS
105 // 28..2f SS zeros filled in by BIOS
106
107 //es:si
108 //eeee0
109 //0ssss
110 //-----
111
112// check for access rights of source & dest here
113
114 // Initialize GDT descriptor
Kevin O'Connor67c00dd2008-03-09 16:10:19 -0400115 SET_SEG(ES, regs->es);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500116 u16 si = regs->si;
117 u16 base15_00 = (regs->es << 4) + si;
118 u16 base23_16 = regs->es >> 12;
Kevin O'Connor67c00dd2008-03-09 16:10:19 -0400119 if (base15_00 < (u16)(regs->es<<4))
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500120 base23_16++;
121 SET_VAR(ES, *(u16*)(si+0x08+0), 47); // limit 15:00 = 6 * 8bytes/descriptor
122 SET_VAR(ES, *(u16*)(si+0x08+2), base15_00);// base 15:00
123 SET_VAR(ES, *(u8 *)(si+0x08+4), base23_16);// base 23:16
124 SET_VAR(ES, *(u8 *)(si+0x08+5), 0x93); // access
125 SET_VAR(ES, *(u16*)(si+0x08+6), 0x0000); // base 31:24/reserved/limit 19:16
126
127 // Initialize CS descriptor
128 SET_VAR(ES, *(u16*)(si+0x20+0), 0xffff);// limit 15:00 = normal 64K limit
129 SET_VAR(ES, *(u16*)(si+0x20+2), 0x0000);// base 15:00
130 SET_VAR(ES, *(u8 *)(si+0x20+4), 0x000f);// base 23:16
131 SET_VAR(ES, *(u8 *)(si+0x20+5), 0x9b); // access
132 SET_VAR(ES, *(u16*)(si+0x20+6), 0x0000);// base 31:24/reserved/limit 19:16
133
134 // Initialize SS descriptor
135 u16 ss = GET_SEG(SS);
136 base15_00 = ss << 4;
137 base23_16 = ss >> 12;
138 SET_VAR(ES, *(u16*)(si+0x28+0), 0xffff); // limit 15:00 = normal 64K limit
139 SET_VAR(ES, *(u16*)(si+0x28+2), base15_00);// base 15:00
140 SET_VAR(ES, *(u8 *)(si+0x28+4), base23_16);// base 23:16
141 SET_VAR(ES, *(u8 *)(si+0x28+5), 0x93); // access
142 SET_VAR(ES, *(u16*)(si+0x28+6), 0x0000); // base 31:24/reserved/limit 19:16
143
Kevin O'Connor2cdd8b62008-03-09 23:37:04 -0400144 u16 count = regs->cx;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500145 asm volatile(
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500146 // Load new descriptor tables
Kevin O'Connor67c00dd2008-03-09 16:10:19 -0400147 "lgdtw %%es:0x8(%%si)\n"
148 "lidtw %%cs:pmode_IDT_info\n"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500149
150 // set PE bit in CR0
151 "movl %%cr0, %%eax\n"
152 "orb $0x01, %%al\n"
153 "movl %%eax, %%cr0\n"
154
155 // far jump to flush CPU queue after transition to protected mode
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500156 "ljmpw $0x0020, $1f\n"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500157 "1:\n"
158
159 // GDT points to valid descriptor table, now load DS, ES
160 "movw $0x10, %%ax\n" // 010 000 = 2nd descriptor in table, TI=GDT, RPL=00
161 "movw %%ax, %%ds\n"
162 "movw $0x18, %%ax\n" // 011 000 = 3rd descriptor in table, TI=GDT, RPL=00
163 "movw %%ax, %%es\n"
164
165 // move CX words from DS:SI to ES:DI
166 "xorw %%si, %%si\n"
167 "xorw %%di, %%di\n"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500168 "rep movsw\n"
169
170 // reset PG bit in CR0 ???
171 "movl %%cr0, %%eax\n"
172 "andb $0xfe, %%al\n"
173 "movl %%eax, %%cr0\n"
174
175 // far jump to flush CPU queue after transition to real mode
176 "ljmpw $0xf000, $2f\n"
177 "2:\n"
178
179 // restore IDT to normal real-mode defaults
180 "lidt %%cs:rmode_IDT_info\n"
181
Kevin O'Connore20ed9f2008-03-01 14:25:44 -0500182 // Restore %ds (from %ss)
183 "movw %%ss, %%ax\n"
184 "movw %%ax, %%ds\n"
Kevin O'Connor2cdd8b62008-03-09 23:37:04 -0400185 : "+c"(count), "+S"(si)
Kevin O'Connored128492008-03-11 11:14:59 -0400186 : : "eax", "di", "cc"); // XXX - also clobbers %es
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500187
188 set_a20(prev_a20_enable);
189
Kevin O'Connor6c781222008-03-09 12:19:23 -0400190 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500191}
192
193// Get the amount of extended memory (above 1M)
194static void
195handle_1588(struct bregs *regs)
196{
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400197 u32 rs = GET_EBDA(ram_size);
198
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500199 // According to Ralf Brown's interrupt the limit should be 15M,
200 // but real machines mostly return max. 63M.
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400201 if (rs > 64*1024*1024)
202 regs->ax = 63 * 1024;
203 else
204 regs->ax = (rs - 1*1024*1024) / 1024;
Kevin O'Connor6c781222008-03-09 12:19:23 -0400205 set_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500206}
207
208// Device busy interrupt. Called by Int 16h when no key available
209static void
210handle_1590(struct bregs *regs)
211{
212}
213
214// Interrupt complete. Called by Int 16h when key becomes available
215static void
216handle_1591(struct bregs *regs)
217{
218}
219
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500220// keyboard intercept
221static void
222handle_154f(struct bregs *regs)
223{
Kevin O'Connordb9e65e2008-06-07 14:41:21 -0400224 set_fail_silent(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500225}
226
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500227static void
228handle_15c0(struct bregs *regs)
229{
230 regs->es = SEG_BIOS;
Kevin O'Connor117fc212008-04-13 18:17:02 -0400231 regs->bx = (u32)&BIOS_CONFIG_TABLE;
Kevin O'Connor6c781222008-03-09 12:19:23 -0400232 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500233}
234
235static void
236handle_15c1(struct bregs *regs)
237{
238 regs->es = GET_BDA(ebda_seg);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400239 set_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500240}
241
242static void
243handle_15e801(struct bregs *regs)
244{
245 // my real system sets ax and bx to 0
246 // this is confirmed by Ralph Brown list
247 // but syslinux v1.48 is known to behave
248 // strangely if ax is set to 0
249 // regs.u.r16.ax = 0;
250 // regs.u.r16.bx = 0;
251
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400252 u32 rs = GET_EBDA(ram_size);
253
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500254 // Get the amount of extended memory (above 1M)
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400255 if (rs > 16*1024*1024) {
256 // limit to 15M
257 regs->cx = 15*1024;
258 // Get the amount of extended memory above 16M in 64k blocks
259 regs->dx = (rs - 16*1024*1024) / (64*1024);
260 } else {
261 regs->cx = (rs - 1*1024*1024) / 1024;
262 regs->dx = 0;
263 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500264
265 // Set configured memory equal to extended memory
266 regs->ax = regs->cx;
267 regs->bx = regs->dx;
268
Kevin O'Connor6c781222008-03-09 12:19:23 -0400269 set_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500270}
271
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500272static void
Kevin O'Connor983d6192008-03-09 12:23:42 -0400273set_e820_range(struct bregs *regs, u32 start, u32 end, u16 type, int last)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500274{
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400275 u32 size = end - start;
276 SET_FARVAR(regs->es, *(u64*)(regs->di+0), start);
277 SET_FARVAR(regs->es, *(u64*)(regs->di+8), size);
Kevin O'Connor983d6192008-03-09 12:23:42 -0400278 SET_FARVAR(regs->es, *(u16*)(regs->di+16), type);
279 SET_FARVAR(regs->es, *(u16*)(regs->di+18), 0x0);
280
281 if (last)
282 regs->ebx = 0;
283 else
284 regs->ebx++;
285 regs->eax = 0x534D4150;
286 regs->ecx = 0x14;
287 set_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500288}
289
290// XXX - should create e820 memory map in post and just copy it here.
291static void
292handle_15e820(struct bregs *regs)
293{
294 if (regs->edx != 0x534D4150) {
Kevin O'Connor6c781222008-03-09 12:19:23 -0400295 set_code_fail(regs, RET_EUNSUPPORTED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500296 return;
297 }
298
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400299 u32 extended_memory_size = GET_EBDA(ram_size);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500300 // greater than EFF00000???
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400301 if (extended_memory_size > 0xf0000000)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500302 // everything after this is reserved memory until we get to 0x100000000
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400303 extended_memory_size = 0xf0000000;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500304
305 switch (regs->bx) {
306 case 0:
Kevin O'Connor983d6192008-03-09 12:23:42 -0400307 set_e820_range(regs, 0x0000000L, 0x0009fc00L, E820_RAM, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500308 break;
309 case 1:
Kevin O'Connor983d6192008-03-09 12:23:42 -0400310 set_e820_range(regs, 0x0009fc00L, 0x000a0000L, E820_RESERVED, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500311 break;
312 case 2:
Kevin O'Connor983d6192008-03-09 12:23:42 -0400313 set_e820_range(regs, 0x000e8000L, 0x00100000L, E820_RESERVED, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500314 break;
315 case 3:
Kevin O'Connor438f6352008-03-30 21:46:53 -0400316 set_e820_range(regs, 0x00100000L
317 , extended_memory_size - CONFIG_ACPI_DATA_SIZE
318 , E820_RAM, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500319 break;
320 case 4:
Kevin O'Connor983d6192008-03-09 12:23:42 -0400321 set_e820_range(regs,
Kevin O'Connor438f6352008-03-30 21:46:53 -0400322 extended_memory_size - CONFIG_ACPI_DATA_SIZE,
Kevin O'Connor983d6192008-03-09 12:23:42 -0400323 extended_memory_size, E820_ACPI, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500324 break;
325 case 5:
326 /* 256KB BIOS area at the end of 4 GB */
Kevin O'Connor983d6192008-03-09 12:23:42 -0400327 set_e820_range(regs, 0xfffc0000L, 0x00000000L, E820_RESERVED, 1);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500328 break;
329 default: /* AX=E820, DX=534D4150, BX unrecognized */
Kevin O'Connor6c781222008-03-09 12:19:23 -0400330 set_code_fail(regs, RET_EUNSUPPORTED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500331 }
332}
333
334static void
335handle_15e8XX(struct bregs *regs)
336{
Kevin O'Connor6c781222008-03-09 12:19:23 -0400337 set_code_fail(regs, RET_EUNSUPPORTED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500338}
339
340static void
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500341handle_15e8(struct bregs *regs)
342{
343 switch (regs->al) {
344 case 0x01: handle_15e801(regs); break;
345 case 0x20: handle_15e820(regs); break;
346 default: handle_15e8XX(regs); break;
347 }
348}
349
350static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500351handle_15XX(struct bregs *regs)
352{
Kevin O'Connor6c781222008-03-09 12:19:23 -0400353 set_code_fail(regs, RET_EUNSUPPORTED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500354}
355
356// INT 15h System Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500357void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500358handle_15(struct bregs *regs)
359{
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500360 //debug_enter(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500361 switch (regs->ah) {
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500362 case 0x24: handle_1524(regs); break;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500363 case 0x4f: handle_154f(regs); break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500364 case 0x52: handle_1552(regs); break;
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500365 case 0x53: handle_1553(regs); break;
366 case 0x83: handle_1583(regs); break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500367 case 0x86: handle_1586(regs); break;
368 case 0x87: handle_1587(regs); break;
369 case 0x88: handle_1588(regs); break;
370 case 0x90: handle_1590(regs); break;
371 case 0x91: handle_1591(regs); break;
372 case 0xc0: handle_15c0(regs); break;
373 case 0xc1: handle_15c1(regs); break;
374 case 0xc2: handle_15c2(regs); break;
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500375 case 0xe8: handle_15e8(regs); break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500376 default: handle_15XX(regs); break;
377 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500378}
379
380// INT 12h Memory Size Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500381void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500382handle_12(struct bregs *regs)
383{
384 debug_enter(regs);
385 regs->ax = GET_BDA(mem_size_kb);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500386}
387
388// INT 11h Equipment List Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500389void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500390handle_11(struct bregs *regs)
391{
392 debug_enter(regs);
393 regs->ax = GET_BDA(equipment_list_flags);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500394}
395
396// INT 05h Print Screen Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500397void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500398handle_05(struct bregs *regs)
399{
400 debug_enter(regs);
401}
402
403// INT 10h Video Support Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500404void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500405handle_10(struct bregs *regs)
406{
407 debug_enter(regs);
408 // dont do anything, since the VGA BIOS handles int10h requests
409}
410
Kevin O'Connor19786762008-03-05 21:09:59 -0500411void VISIBLE16
Kevin O'Connored128492008-03-11 11:14:59 -0400412handle_nmi()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500413{
Kevin O'Connored128492008-03-11 11:14:59 -0400414 debug_isr();
415 BX_PANIC("NMI Handler called\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500416}
417
418// INT 75 - IRQ13 - MATH COPROCESSOR EXCEPTION
Kevin O'Connor19786762008-03-05 21:09:59 -0500419void VISIBLE16
Kevin O'Connored128492008-03-11 11:14:59 -0400420handle_75()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500421{
Kevin O'Connored128492008-03-11 11:14:59 -0400422 debug_isr();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500423
424 // clear irq13
425 outb(0, PORT_MATH_CLEAR);
426 // clear interrupt
427 eoi_both_pics();
428 // legacy nmi call
429 struct bregs br;
430 memset(&br, 0, sizeof(br));
431 call16_int(0x02, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500432}