blob: 79f7dc8696d11d2abfc9f9cd7ca536d31521434e [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
Kevin O'Connor3d029412008-03-05 20:43:38 -05008#include "acpi.h"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05009#include "util.h" // irq_restore
10#include "biosvar.h" // CONFIG_BIOS_TABLE
11#include "ioport.h" // inb
12#include "cmos.h" // inb_cmos
13
Kevin O'Connore2e5f012008-03-08 10:27:39 -050014#define E820_RAM 1
15#define E820_RESERVED 2
16#define E820_ACPI 3
17#define E820_NVS 4
18#define E820_UNUSABLE 5
19
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050020// Use PS2 System Control port A to set A20 enable
21static inline u8
22set_a20(u8 cond)
23{
24 // get current setting first
25 u8 newval, oldval = inb(PORT_A20);
26 if (cond)
27 newval = oldval | 0x02;
28 else
29 newval = oldval & ~0x02;
30 outb(newval, PORT_A20);
31
32 return (newval & 0x02) != 0;
33}
34
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050035static void
36handle_152400(struct bregs *regs)
37{
38 set_a20(0);
39 handle_ret(regs, 0);
40}
41
42static void
43handle_152401(struct bregs *regs)
44{
45 set_a20(1);
46 handle_ret(regs, 0);
47}
48
49static void
50handle_152402(struct bregs *regs)
51{
52 regs->al = !!(inb(PORT_A20) & 0x20);
53 handle_ret(regs, 0);
54}
55
56static void
57handle_152403(struct bregs *regs)
58{
59 regs->bx = 3;
60 handle_ret(regs, 0);
61}
62
63static void
64handle_1524XX(struct bregs *regs)
65{
66 handle_ret(regs, RET_EUNSUPPORTED);
67}
68
Kevin O'Connoradb6b372008-03-01 13:38:38 -050069static void
70handle_1524(struct bregs *regs)
71{
72 switch (regs->al) {
73 case 0x00: handle_152400(regs); break;
74 case 0x01: handle_152401(regs); break;
75 case 0x02: handle_152402(regs); break;
76 case 0x03: handle_152403(regs); break;
77 default: handle_1524XX(regs); break;
78 }
79}
80
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050081// removable media eject
82static void
83handle_1552(struct bregs *regs)
84{
85 handle_ret(regs, 0);
86}
87
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050088// 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;
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -050094 u8 kbd = inb(PORT_PS2_CTRLB);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050095 while (count)
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -050096 if ((inb(PORT_PS2_CTRLB) ^ kbd) & KBD_REFRESH)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050097 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(
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500168 // Load new descriptor tables
169 "lgdt %%es:(%1)\n"
170 "lidt %%cs:pmode_IDT_info\n"
171
172 // set PE bit in CR0
173 "movl %%cr0, %%eax\n"
174 "orb $0x01, %%al\n"
175 "movl %%eax, %%cr0\n"
176
177 // far jump to flush CPU queue after transition to protected mode
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500178 "ljmpw $0x0020, $1f\n"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500179 "1:\n"
180
181 // GDT points to valid descriptor table, now load DS, ES
182 "movw $0x10, %%ax\n" // 010 000 = 2nd descriptor in table, TI=GDT, RPL=00
183 "movw %%ax, %%ds\n"
184 "movw $0x18, %%ax\n" // 011 000 = 3rd descriptor in table, TI=GDT, RPL=00
185 "movw %%ax, %%es\n"
186
187 // move CX words from DS:SI to ES:DI
188 "xorw %%si, %%si\n"
189 "xorw %%di, %%di\n"
190 "cld\n"
191 "rep movsw\n"
192
193 // reset PG bit in CR0 ???
194 "movl %%cr0, %%eax\n"
195 "andb $0xfe, %%al\n"
196 "movl %%eax, %%cr0\n"
197
198 // far jump to flush CPU queue after transition to real mode
199 "ljmpw $0xf000, $2f\n"
200 "2:\n"
201
202 // restore IDT to normal real-mode defaults
203 "lidt %%cs:rmode_IDT_info\n"
204
Kevin O'Connore20ed9f2008-03-01 14:25:44 -0500205 // Restore %ds (from %ss)
206 "movw %%ss, %%ax\n"
207 "movw %%ax, %%ds\n"
208 : : "c" (regs->cx), "r" (si + 8)
209 : "eax", "di", "si"); // XXX - also clobbers %es
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500210
211 set_a20(prev_a20_enable);
212
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500213 handle_ret(regs, 0);
214}
215
216// Get the amount of extended memory (above 1M)
217static void
218handle_1588(struct bregs *regs)
219{
Kevin O'Connore09b9822008-03-06 19:16:37 -0500220 regs->al = inb_cmos(CMOS_MEM_EXTMEM_LOW);
221 regs->ah = inb_cmos(CMOS_MEM_EXTMEM_HIGH);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500222 // According to Ralf Brown's interrupt the limit should be 15M,
223 // but real machines mostly return max. 63M.
224 if (regs->ax > 0xffc0)
225 regs->ax = 0xffc0;
226 set_cf(regs, 0);
227}
228
229// Device busy interrupt. Called by Int 16h when no key available
230static void
231handle_1590(struct bregs *regs)
232{
233}
234
235// Interrupt complete. Called by Int 16h when key becomes available
236static void
237handle_1591(struct bregs *regs)
238{
239}
240
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500241// keyboard intercept
242static void
243handle_154f(struct bregs *regs)
244{
245 set_cf(regs, 1);
246}
247
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500248static void
249handle_15c0(struct bregs *regs)
250{
251 regs->es = SEG_BIOS;
252 regs->bx = (u16)&BIOS_CONFIG_TABLE;
Kevin O'Connor44c631d2008-03-02 11:24:36 -0500253 handle_ret(regs, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500254}
255
256static void
257handle_15c1(struct bregs *regs)
258{
259 regs->es = GET_BDA(ebda_seg);
260 set_cf(regs, 0);
261}
262
263static void
264handle_15e801(struct bregs *regs)
265{
266 // my real system sets ax and bx to 0
267 // this is confirmed by Ralph Brown list
268 // but syslinux v1.48 is known to behave
269 // strangely if ax is set to 0
270 // regs.u.r16.ax = 0;
271 // regs.u.r16.bx = 0;
272
273 // Get the amount of extended memory (above 1M)
Kevin O'Connore09b9822008-03-06 19:16:37 -0500274 regs->cl = inb_cmos(CMOS_MEM_EXTMEM_LOW);
275 regs->ch = inb_cmos(CMOS_MEM_EXTMEM_HIGH);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500276
277 // limit to 15M
278 if (regs->cx > 0x3c00)
279 regs->cx = 0x3c00;
280
281 // Get the amount of extended memory above 16M in 64k blocs
Kevin O'Connore09b9822008-03-06 19:16:37 -0500282 regs->dl = inb_cmos(CMOS_MEM_EXTMEM2_LOW);
283 regs->dh = inb_cmos(CMOS_MEM_EXTMEM2_HIGH);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500284
285 // Set configured memory equal to extended memory
286 regs->ax = regs->cx;
287 regs->bx = regs->dx;
288
289 set_cf(regs, 0);
290}
291
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500292static void
293set_e820_range(u16 DI, u32 start, u32 end, u16 type)
294{
295 SET_VAR(ES, *(u16*)(DI+0), start);
296 SET_VAR(ES, *(u16*)(DI+2), start >> 16);
297 SET_VAR(ES, *(u16*)(DI+4), 0x00);
298 SET_VAR(ES, *(u16*)(DI+6), 0x00);
299
300 end -= start;
301 SET_VAR(ES, *(u16*)(DI+8), end);
302 SET_VAR(ES, *(u16*)(DI+10), end >> 16);
303 SET_VAR(ES, *(u16*)(DI+12), 0x0000);
304 SET_VAR(ES, *(u16*)(DI+14), 0x0000);
305
306 SET_VAR(ES, *(u16*)(DI+16), type);
307 SET_VAR(ES, *(u16*)(DI+18), 0x0);
308}
309
310// XXX - should create e820 memory map in post and just copy it here.
311static void
312handle_15e820(struct bregs *regs)
313{
314 if (regs->edx != 0x534D4150) {
315 handle_ret(regs, RET_EUNSUPPORTED);
316 return;
317 }
318
Kevin O'Connore09b9822008-03-06 19:16:37 -0500319 u32 extended_memory_size = inb_cmos(CMOS_MEM_EXTMEM2_HIGH);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500320 extended_memory_size <<= 8;
Kevin O'Connore09b9822008-03-06 19:16:37 -0500321 extended_memory_size |= inb_cmos(CMOS_MEM_EXTMEM2_LOW);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500322 extended_memory_size *= 64;
323 // greater than EFF00000???
324 if (extended_memory_size > 0x3bc000)
325 // everything after this is reserved memory until we get to 0x100000000
326 extended_memory_size = 0x3bc000;
327 extended_memory_size *= 1024;
328 extended_memory_size += (16L * 1024 * 1024);
329
330 if (extended_memory_size <= (16L * 1024 * 1024)) {
Kevin O'Connore09b9822008-03-06 19:16:37 -0500331 extended_memory_size = inb_cmos(CMOS_MEM_EXTMEM_HIGH);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500332 extended_memory_size <<= 8;
Kevin O'Connore09b9822008-03-06 19:16:37 -0500333 extended_memory_size |= inb_cmos(CMOS_MEM_EXTMEM_LOW);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500334 extended_memory_size *= 1024;
335 }
336
337 switch (regs->bx) {
338 case 0:
Kevin O'Connore2e5f012008-03-08 10:27:39 -0500339 set_e820_range(regs->di, 0x0000000L, 0x0009fc00L, E820_RAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500340 regs->ebx = 1;
341 regs->eax = 0x534D4150;
342 regs->ecx = 0x14;
343 set_cf(regs, 0);
344 break;
345 case 1:
Kevin O'Connore2e5f012008-03-08 10:27:39 -0500346 set_e820_range(regs->di, 0x0009fc00L, 0x000a0000L, E820_RESERVED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500347 regs->ebx = 2;
348 regs->eax = 0x534D4150;
349 regs->ecx = 0x14;
350 set_cf(regs, 0);
351 break;
352 case 2:
Kevin O'Connore2e5f012008-03-08 10:27:39 -0500353 set_e820_range(regs->di, 0x000e8000L, 0x00100000L, E820_RESERVED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500354 regs->ebx = 3;
355 regs->eax = 0x534D4150;
356 regs->ecx = 0x14;
357 set_cf(regs, 0);
358 break;
359 case 3:
360 set_e820_range(regs->di, 0x00100000L,
Kevin O'Connore2e5f012008-03-08 10:27:39 -0500361 extended_memory_size - ACPI_DATA_SIZE, E820_RAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500362 regs->ebx = 4;
363 regs->eax = 0x534D4150;
364 regs->ecx = 0x14;
365 set_cf(regs, 0);
366 break;
367 case 4:
368 set_e820_range(regs->di,
369 extended_memory_size - ACPI_DATA_SIZE,
Kevin O'Connore2e5f012008-03-08 10:27:39 -0500370 extended_memory_size, E820_ACPI); // ACPI RAM
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500371 regs->ebx = 5;
372 regs->eax = 0x534D4150;
373 regs->ecx = 0x14;
374 set_cf(regs, 0);
375 break;
376 case 5:
377 /* 256KB BIOS area at the end of 4 GB */
Kevin O'Connore2e5f012008-03-08 10:27:39 -0500378 set_e820_range(regs->di, 0xfffc0000L, 0x00000000L, E820_RESERVED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500379 regs->ebx = 0;
380 regs->eax = 0x534D4150;
381 regs->ecx = 0x14;
382 set_cf(regs, 0);
383 break;
384 default: /* AX=E820, DX=534D4150, BX unrecognized */
385 handle_ret(regs, RET_EUNSUPPORTED);
386 }
387}
388
389static void
390handle_15e8XX(struct bregs *regs)
391{
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500392 handle_ret(regs, RET_EUNSUPPORTED);
393}
394
395static void
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500396handle_15e8(struct bregs *regs)
397{
398 switch (regs->al) {
399 case 0x01: handle_15e801(regs); break;
400 case 0x20: handle_15e820(regs); break;
401 default: handle_15e8XX(regs); break;
402 }
403}
404
405static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500406handle_15XX(struct bregs *regs)
407{
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500408 handle_ret(regs, RET_EUNSUPPORTED);
409}
410
411// INT 15h System Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500412void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500413handle_15(struct bregs *regs)
414{
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500415 //debug_enter(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500416 switch (regs->ah) {
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500417 case 0x24: handle_1524(regs); break;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500418 case 0x4f: handle_154f(regs); break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500419 case 0x52: handle_1552(regs); break;
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500420 case 0x53: handle_1553(regs); break;
421 case 0x83: handle_1583(regs); break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500422 case 0x86: handle_1586(regs); break;
423 case 0x87: handle_1587(regs); break;
424 case 0x88: handle_1588(regs); break;
425 case 0x90: handle_1590(regs); break;
426 case 0x91: handle_1591(regs); break;
427 case 0xc0: handle_15c0(regs); break;
428 case 0xc1: handle_15c1(regs); break;
429 case 0xc2: handle_15c2(regs); break;
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500430 case 0xe8: handle_15e8(regs); break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500431 default: handle_15XX(regs); break;
432 }
433 debug_exit(regs);
434}
435
436// INT 12h Memory Size Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500437void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500438handle_12(struct bregs *regs)
439{
440 debug_enter(regs);
441 regs->ax = GET_BDA(mem_size_kb);
442 debug_exit(regs);
443}
444
445// INT 11h Equipment List Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500446void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500447handle_11(struct bregs *regs)
448{
449 debug_enter(regs);
450 regs->ax = GET_BDA(equipment_list_flags);
451 debug_exit(regs);
452}
453
454// INT 05h Print Screen Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500455void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500456handle_05(struct bregs *regs)
457{
458 debug_enter(regs);
459}
460
461// INT 10h Video Support Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500462void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500463handle_10(struct bregs *regs)
464{
465 debug_enter(regs);
466 // dont do anything, since the VGA BIOS handles int10h requests
467}
468
Kevin O'Connor19786762008-03-05 21:09:59 -0500469void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500470handle_nmi(struct bregs *regs)
471{
Kevin O'Connorc65a3802008-03-02 13:58:23 -0500472 debug_isr(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500473 // XXX
474}
475
476// INT 75 - IRQ13 - MATH COPROCESSOR EXCEPTION
Kevin O'Connor19786762008-03-05 21:09:59 -0500477void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500478handle_75(struct bregs *regs)
479{
Kevin O'Connorc65a3802008-03-02 13:58:23 -0500480 debug_isr(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500481
482 // clear irq13
483 outb(0, PORT_MATH_CLEAR);
484 // clear interrupt
485 eoi_both_pics();
486 // legacy nmi call
487 struct bregs br;
488 memset(&br, 0, sizeof(br));
489 call16_int(0x02, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500490}