Version 0.1.2
[seabios.git] / src / system.c
1 // 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
9 #include "biosvar.h" // CONFIG_BIOS_TABLE
10 #include "ioport.h" // inb
11 #include "cmos.h" // inb_cmos
12
13 #define RET_EUNSUPPORTED 0x86
14
15
16 // Use PS2 System Control port A to set A20 enable
17 static inline u8
18 set_a20(u8 cond)
19 {
20     // get current setting first
21     u8 newval, oldval = inb(PORT_A20);
22     if (cond)
23         newval = oldval | 0x02;
24     else
25         newval = oldval & ~0x02;
26     outb(newval, PORT_A20);
27
28     return (newval & 0x02) != 0;
29 }
30
31 static inline void
32 handle_ret(struct bregs *regs, u8 code)
33 {
34     regs->ah = code;
35     set_cf(regs, code);
36 }
37
38 static void
39 handle_152400(struct bregs *regs)
40 {
41     set_a20(0);
42     handle_ret(regs, 0);
43 }
44
45 static void
46 handle_152401(struct bregs *regs)
47 {
48     set_a20(1);
49     handle_ret(regs, 0);
50 }
51
52 static void
53 handle_152402(struct bregs *regs)
54 {
55     regs->al = !!(inb(PORT_A20) & 0x20);
56     handle_ret(regs, 0);
57 }
58
59 static void
60 handle_152403(struct bregs *regs)
61 {
62     regs->bx = 3;
63     handle_ret(regs, 0);
64 }
65
66 static void
67 handle_1524XX(struct bregs *regs)
68 {
69     handle_ret(regs, RET_EUNSUPPORTED);
70 }
71
72 // removable media eject
73 static void
74 handle_1552(struct bregs *regs)
75 {
76     handle_ret(regs, 0);
77 }
78
79 // Set Interval requested.
80 static void
81 handle_158300(struct bregs *regs)
82 {
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);
87     }
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);
94
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);
101
102     set_cf(regs, 0); // XXX - no set ah?
103 }
104
105 // Clear interval requested
106 static void
107 handle_158301(struct bregs *regs)
108 {
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?
114 }
115
116 static void
117 handle_1583XX(struct bregs *regs)
118 {
119     regs->al--;
120     handle_ret(regs, RET_EUNSUPPORTED);
121 }
122
123 // Sleep for n microseconds. currently using the
124 // refresh request port 0x61 bit4, toggling every 15usec
125 static void
126 usleep(u32 count)
127 {
128     count = count / 15;
129     u8 kbd = inb(PORT_KBD_CTRLB);
130     while (count)
131         if ((inb(PORT_KBD_CTRLB) ^ kbd) & KBD_REFRESH)
132             count--;
133 }
134
135 // Wait for CX:DX microseconds. currently using the
136 // refresh request port 0x61 bit4, toggling every 15usec
137 static void
138 handle_1586(struct bregs *regs)
139 {
140     irq_enable();
141     usleep((regs->cx << 16) | regs->dx);
142     irq_disable();
143 }
144
145 static void
146 handle_1587(struct bregs *regs)
147 {
148     // +++ should probably have descriptor checks
149     // +++ should have exception handlers
150
151     // turn off interrupts
152     unsigned long flags = irq_save();
153
154     u8 prev_a20_enable = set_a20(1); // enable A20 line
155
156     // 128K max of transfer on 386+ ???
157     // source == destination ???
158
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
168
169     //es:si
170     //eeee0
171     //0ssss
172     //-----
173
174 // check for access rights of source & dest here
175
176     // Initialize GDT descriptor
177     u16 si = regs->si;
178     u16 base15_00 = (regs->es << 4) + si;
179     u16 base23_16 = regs->es >> 12;
180     if (base15_00 < (regs->es<<4))
181         base23_16++;
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
187
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
194
195     // Initialize SS descriptor
196     u16 ss = GET_SEG(SS);
197     base15_00 = ss << 4;
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
204
205     asm volatile(
206         // Save registers
207         "pushw %%ds\n"
208         "pushw %%es\n"
209         "pushal\n"
210
211         // Load new descriptor tables
212         "lgdt %%es:(%1)\n"
213         "lidt %%cs:pmode_IDT_info\n"
214
215         // set PE bit in CR0
216         "movl %%cr0, %%eax\n"
217         "orb $0x01, %%al\n"
218         "movl %%eax, %%cr0\n"
219
220         // far jump to flush CPU queue after transition to protected mode
221         "ljmpw $0xf000, $1f\n"
222         "1:\n"
223
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
226         "movw %%ax, %%ds\n"
227         "movw $0x18, %%ax\n" // 011 000 = 3rd descriptor in table, TI=GDT, RPL=00
228         "movw %%ax, %%es\n"
229
230         // move CX words from DS:SI to ES:DI
231         "xorw %%si, %%si\n"
232         "xorw %%di, %%di\n"
233         "cld\n"
234         "rep movsw\n"
235
236         // reset PG bit in CR0 ???
237         "movl %%cr0, %%eax\n"
238         "andb $0xfe, %%al\n"
239         "movl %%eax, %%cr0\n"
240
241         // far jump to flush CPU queue after transition to real mode
242         "ljmpw $0xf000, $2f\n"
243         "2:\n"
244
245         // restore IDT to normal real-mode defaults
246         "lidt %%cs:rmode_IDT_info\n"
247
248         // restore regisers
249         "popal\n"
250         "popw %%es\n"
251         "popw %%ds\n" : : "c" (regs->cx), "r" (si + 8));
252
253     set_a20(prev_a20_enable);
254
255     irq_restore(flags);
256
257     handle_ret(regs, 0);
258 }
259
260 // Get the amount of extended memory (above 1M)
261 static void
262 handle_1588(struct bregs *regs)
263 {
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)
269         regs->ax = 0xffc0;
270     set_cf(regs, 0);
271 }
272
273 // Device busy interrupt.  Called by Int 16h when no key available
274 static void
275 handle_1590(struct bregs *regs)
276 {
277 }
278
279 // Interrupt complete.  Called by Int 16h when key becomes available
280 static void
281 handle_1591(struct bregs *regs)
282 {
283 }
284
285 // keyboard intercept
286 static void
287 handle_154f(struct bregs *regs)
288 {
289     set_cf(regs, 1);
290 }
291
292 static void
293 handle_15c0(struct bregs *regs)
294 {
295     regs->es = SEG_BIOS;
296     regs->bx = (u16)&BIOS_CONFIG_TABLE;
297 }
298
299 static void
300 handle_15c1(struct bregs *regs)
301 {
302     regs->es = GET_BDA(ebda_seg);
303     set_cf(regs, 0);
304 }
305
306 static void
307 handle_15e801(struct bregs *regs)
308 {
309     // my real system sets ax and bx to 0
310     // this is confirmed by Ralph Brown list
311     // but syslinux v1.48 is known to behave
312     // strangely if ax is set to 0
313     // regs.u.r16.ax = 0;
314     // regs.u.r16.bx = 0;
315
316     // Get the amount of extended memory (above 1M)
317     regs->cl = inb_cmos(CMOS_EXTMEM_LOW);
318     regs->ch = inb_cmos(CMOS_EXTMEM_HIGH);
319
320     // limit to 15M
321     if (regs->cx > 0x3c00)
322         regs->cx = 0x3c00;
323
324     // Get the amount of extended memory above 16M in 64k blocs
325     regs->dl = inb_cmos(CMOS_EXTMEM2_LOW);
326     regs->dh = inb_cmos(CMOS_EXTMEM2_HIGH);
327
328     // Set configured memory equal to extended memory
329     regs->ax = regs->cx;
330     regs->bx = regs->dx;
331
332     set_cf(regs, 0);
333 }
334
335 #define ACPI_DATA_SIZE    0x00010000L
336
337 static void
338 set_e820_range(u16 DI, u32 start, u32 end, u16 type)
339 {
340     SET_VAR(ES, *(u16*)(DI+0), start);
341     SET_VAR(ES, *(u16*)(DI+2), start >> 16);
342     SET_VAR(ES, *(u16*)(DI+4), 0x00);
343     SET_VAR(ES, *(u16*)(DI+6), 0x00);
344
345     end -= start;
346     SET_VAR(ES, *(u16*)(DI+8), end);
347     SET_VAR(ES, *(u16*)(DI+10), end >> 16);
348     SET_VAR(ES, *(u16*)(DI+12), 0x0000);
349     SET_VAR(ES, *(u16*)(DI+14), 0x0000);
350
351     SET_VAR(ES, *(u16*)(DI+16), type);
352     SET_VAR(ES, *(u16*)(DI+18), 0x0);
353 }
354
355 // XXX - should create e820 memory map in post and just copy it here.
356 static void
357 handle_15e820(struct bregs *regs)
358 {
359     if (regs->edx != 0x534D4150) {
360         handle_ret(regs, RET_EUNSUPPORTED);
361         return;
362     }
363
364     u32 extended_memory_size = inb_cmos(CMOS_EXTMEM2_HIGH);
365     extended_memory_size <<= 8;
366     extended_memory_size |= inb_cmos(CMOS_EXTMEM2_LOW);
367     extended_memory_size *= 64;
368     // greater than EFF00000???
369     if (extended_memory_size > 0x3bc000)
370         // everything after this is reserved memory until we get to 0x100000000
371         extended_memory_size = 0x3bc000;
372     extended_memory_size *= 1024;
373     extended_memory_size += (16L * 1024 * 1024);
374
375     if (extended_memory_size <= (16L * 1024 * 1024)) {
376         extended_memory_size = inb_cmos(CMOS_EXTMEM_HIGH);
377         extended_memory_size <<= 8;
378         extended_memory_size |= inb_cmos(CMOS_EXTMEM_LOW);
379         extended_memory_size *= 1024;
380     }
381
382     switch (regs->bx) {
383     case 0:
384         set_e820_range(regs->di, 0x0000000L, 0x0009fc00L, 1);
385         regs->ebx = 1;
386         regs->eax = 0x534D4150;
387         regs->ecx = 0x14;
388         set_cf(regs, 0);
389         break;
390     case 1:
391         set_e820_range(regs->di, 0x0009fc00L, 0x000a0000L, 2);
392         regs->ebx = 2;
393         regs->eax = 0x534D4150;
394         regs->ecx = 0x14;
395         set_cf(regs, 0);
396         break;
397     case 2:
398         set_e820_range(regs->di, 0x000e8000L, 0x00100000L, 2);
399         regs->ebx = 3;
400         regs->eax = 0x534D4150;
401         regs->ecx = 0x14;
402         set_cf(regs, 0);
403         break;
404     case 3:
405         set_e820_range(regs->di, 0x00100000L,
406                        extended_memory_size - ACPI_DATA_SIZE, 1);
407         regs->ebx = 4;
408         regs->eax = 0x534D4150;
409         regs->ecx = 0x14;
410         set_cf(regs, 0);
411         break;
412     case 4:
413         set_e820_range(regs->di,
414                        extended_memory_size - ACPI_DATA_SIZE,
415                        extended_memory_size, 3); // ACPI RAM
416         regs->ebx = 5;
417         regs->eax = 0x534D4150;
418         regs->ecx = 0x14;
419         set_cf(regs, 0);
420         break;
421     case 5:
422         /* 256KB BIOS area at the end of 4 GB */
423         set_e820_range(regs->di, 0xfffc0000L, 0x00000000L, 2);
424         regs->ebx = 0;
425         regs->eax = 0x534D4150;
426         regs->ecx = 0x14;
427         set_cf(regs, 0);
428         break;
429     default:  /* AX=E820, DX=534D4150, BX unrecognized */
430         handle_ret(regs, RET_EUNSUPPORTED);
431     }
432 }
433
434 static void
435 handle_15e8XX(struct bregs *regs)
436 {
437     regs->al--;
438     handle_ret(regs, RET_EUNSUPPORTED);
439 }
440
441 static void
442 handle_15XX(struct bregs *regs)
443 {
444     regs->al--;
445     handle_ret(regs, RET_EUNSUPPORTED);
446 }
447
448 // INT 15h System Services Entry Point
449 void VISIBLE
450 handle_15(struct bregs *regs)
451 {
452     debug_enter(regs);
453     switch (regs->ah) {
454     case 0x24:
455         switch (regs->al) {
456         case 0x00: handle_152400(regs); break;
457         case 0x01: handle_152401(regs); break;
458         case 0x02: handle_152402(regs); break;
459         case 0x03: handle_152403(regs); break;
460         default:   handle_1524XX(regs); break;
461         }
462         break;
463     case 0x4f: handle_154f(regs); break;
464     case 0x52: handle_1552(regs); break;
465     case 0x83:
466         switch (regs->al) {
467         case 0x00: handle_158300(regs); break;
468         case 0x01: handle_158301(regs); break;
469         default:   handle_1583XX(regs); break;
470         }
471         break;
472     case 0x86: handle_1586(regs); break;
473     case 0x87: handle_1587(regs); break;
474     case 0x88: handle_1588(regs); break;
475     case 0x90: handle_1590(regs); break;
476     case 0x91: handle_1591(regs); break;
477     case 0xc0: handle_15c0(regs); break;
478     case 0xc1: handle_15c1(regs); break;
479     case 0xc2: handle_15c2(regs); break;
480     case 0xe8:
481         switch (regs->al) {
482         case 0x01: handle_15e801(regs); break;
483         case 0x20: handle_15e820(regs); break;
484         default:   handle_15e8XX(regs); break;
485         }
486         break;
487     default:   handle_15XX(regs); break;
488     }
489     debug_exit(regs);
490 }
491
492 // INT 12h Memory Size Service Entry Point
493 void VISIBLE
494 handle_12(struct bregs *regs)
495 {
496     debug_enter(regs);
497     regs->ax = GET_BDA(mem_size_kb);
498     debug_exit(regs);
499 }
500
501 // INT 11h Equipment List Service Entry Point
502 void VISIBLE
503 handle_11(struct bregs *regs)
504 {
505     debug_enter(regs);
506     regs->ax = GET_BDA(equipment_list_flags);
507     debug_exit(regs);
508 }
509
510 // INT 05h Print Screen Service Entry Point
511 void VISIBLE
512 handle_05(struct bregs *regs)
513 {
514     debug_enter(regs);
515 }
516
517 // INT 10h Video Support Service Entry Point
518 void VISIBLE
519 handle_10(struct bregs *regs)
520 {
521     debug_enter(regs);
522     // dont do anything, since the VGA BIOS handles int10h requests
523 }
524
525 void VISIBLE
526 handle_nmi(struct bregs *regs)
527 {
528     debug_enter(regs);
529     // XXX
530 }
531
532 // INT 75 - IRQ13 - MATH COPROCESSOR EXCEPTION
533 void VISIBLE
534 handle_75(struct bregs *regs)
535 {
536     debug_enter(regs);
537
538     // clear irq13
539     outb(0, PORT_MATH_CLEAR);
540     // clear interrupt
541     eoi_both_pics();
542     // legacy nmi call
543     struct bregs br;
544     memset(&br, 0, sizeof(br));
545     call16_int(0x02, &br);
546 }