8c595fc1d502f6657867a125fa91476ecb9c1ca3
[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 E820_RAM          1
14 #define E820_RESERVED     2
15 #define E820_ACPI         3
16 #define E820_NVS          4
17 #define E820_UNUSABLE     5
18
19 // Use PS2 System Control port A to set A20 enable
20 static inline u8
21 set_a20(u8 cond)
22 {
23     // get current setting first
24     u8 newval, oldval = inb(PORT_A20);
25     if (cond)
26         newval = oldval | 0x02;
27     else
28         newval = oldval & ~0x02;
29     outb(newval, PORT_A20);
30
31     return (newval & 0x02) != 0;
32 }
33
34 static void
35 handle_152400(struct bregs *regs)
36 {
37     set_a20(0);
38     set_code_success(regs);
39 }
40
41 static void
42 handle_152401(struct bregs *regs)
43 {
44     set_a20(1);
45     set_code_success(regs);
46 }
47
48 static void
49 handle_152402(struct bregs *regs)
50 {
51     regs->al = !!(inb(PORT_A20) & 0x20);
52     set_code_success(regs);
53 }
54
55 static void
56 handle_152403(struct bregs *regs)
57 {
58     regs->bx = 3;
59     set_code_success(regs);
60 }
61
62 static void
63 handle_1524XX(struct bregs *regs)
64 {
65     set_code_fail(regs, RET_EUNSUPPORTED);
66 }
67
68 static void
69 handle_1524(struct bregs *regs)
70 {
71     switch (regs->al) {
72     case 0x00: handle_152400(regs); break;
73     case 0x01: handle_152401(regs); break;
74     case 0x02: handle_152402(regs); break;
75     case 0x03: handle_152403(regs); break;
76     default:   handle_1524XX(regs); break;
77     }
78 }
79
80 // removable media eject
81 static void
82 handle_1552(struct bregs *regs)
83 {
84     set_code_success(regs);
85 }
86
87 // Wait for CX:DX microseconds. currently using the
88 // refresh request port 0x61 bit4, toggling every 15usec
89 static void
90 handle_1586(struct bregs *regs)
91 {
92     irq_enable();
93     usleep((regs->cx << 16) | regs->dx);
94     irq_disable();
95 }
96
97 static void
98 handle_1587(struct bregs *regs)
99 {
100     // +++ should probably have descriptor checks
101     // +++ should have exception handlers
102
103     u8 prev_a20_enable = set_a20(1); // enable A20 line
104
105     // 128K max of transfer on 386+ ???
106     // source == destination ???
107
108     // ES:SI points to descriptor table
109     // offset   use     initially  comments
110     // ==============================================
111     // 00..07   Unused  zeros      Null descriptor
112     // 08..0f   GDT     zeros      filled in by BIOS
113     // 10..17   source  ssssssss   source of data
114     // 18..1f   dest    dddddddd   destination of data
115     // 20..27   CS      zeros      filled in by BIOS
116     // 28..2f   SS      zeros      filled in by BIOS
117
118     //es:si
119     //eeee0
120     //0ssss
121     //-----
122
123 // check for access rights of source & dest here
124
125     // Initialize GDT descriptor
126     SET_SEG(ES, regs->es);
127     u16 si = regs->si;
128     u16 base15_00 = (regs->es << 4) + si;
129     u16 base23_16 = regs->es >> 12;
130     if (base15_00 < (u16)(regs->es<<4))
131         base23_16++;
132     SET_VAR(ES, *(u16*)(si+0x08+0), 47);       // limit 15:00 = 6 * 8bytes/descriptor
133     SET_VAR(ES, *(u16*)(si+0x08+2), base15_00);// base 15:00
134     SET_VAR(ES, *(u8 *)(si+0x08+4), base23_16);// base 23:16
135     SET_VAR(ES, *(u8 *)(si+0x08+5), 0x93);     // access
136     SET_VAR(ES, *(u16*)(si+0x08+6), 0x0000);   // base 31:24/reserved/limit 19:16
137
138     // Initialize CS descriptor
139     SET_VAR(ES, *(u16*)(si+0x20+0), 0xffff);// limit 15:00 = normal 64K limit
140     SET_VAR(ES, *(u16*)(si+0x20+2), 0x0000);// base 15:00
141     SET_VAR(ES, *(u8 *)(si+0x20+4), 0x000f);// base 23:16
142     SET_VAR(ES, *(u8 *)(si+0x20+5), 0x9b);  // access
143     SET_VAR(ES, *(u16*)(si+0x20+6), 0x0000);// base 31:24/reserved/limit 19:16
144
145     // Initialize SS descriptor
146     u16 ss = GET_SEG(SS);
147     base15_00 = ss << 4;
148     base23_16 = ss >> 12;
149     SET_VAR(ES, *(u16*)(si+0x28+0), 0xffff);   // limit 15:00 = normal 64K limit
150     SET_VAR(ES, *(u16*)(si+0x28+2), base15_00);// base 15:00
151     SET_VAR(ES, *(u8 *)(si+0x28+4), base23_16);// base 23:16
152     SET_VAR(ES, *(u8 *)(si+0x28+5), 0x93);     // access
153     SET_VAR(ES, *(u16*)(si+0x28+6), 0x0000);   // base 31:24/reserved/limit 19:16
154
155     asm volatile(
156         // Load new descriptor tables
157         "lgdtw %%es:0x8(%%si)\n"
158         "lidtw %%cs:pmode_IDT_info\n"
159
160         // set PE bit in CR0
161         "movl %%cr0, %%eax\n"
162         "orb $0x01, %%al\n"
163         "movl %%eax, %%cr0\n"
164
165         // far jump to flush CPU queue after transition to protected mode
166         "ljmpw $0x0020, $1f\n"
167         "1:\n"
168
169         // GDT points to valid descriptor table, now load DS, ES
170         "movw $0x10, %%ax\n" // 010 000 = 2nd descriptor in table, TI=GDT, RPL=00
171         "movw %%ax, %%ds\n"
172         "movw $0x18, %%ax\n" // 011 000 = 3rd descriptor in table, TI=GDT, RPL=00
173         "movw %%ax, %%es\n"
174
175         // move CX words from DS:SI to ES:DI
176         "xorw %%si, %%si\n"
177         "xorw %%di, %%di\n"
178         "rep movsw\n"
179
180         // reset PG bit in CR0 ???
181         "movl %%cr0, %%eax\n"
182         "andb $0xfe, %%al\n"
183         "movl %%eax, %%cr0\n"
184
185         // far jump to flush CPU queue after transition to real mode
186         "ljmpw $0xf000, $2f\n"
187         "2:\n"
188
189         // restore IDT to normal real-mode defaults
190         "lidt %%cs:rmode_IDT_info\n"
191
192         // Restore %ds (from %ss)
193         "movw %%ss, %%ax\n"
194         "movw %%ax, %%ds\n"
195         : "+c"(regs->cx), "+S"(si)
196         : : "eax", "di"); // XXX - also clobbers %es
197
198     set_a20(prev_a20_enable);
199
200     set_code_success(regs);
201 }
202
203 // Get the amount of extended memory (above 1M)
204 static void
205 handle_1588(struct bregs *regs)
206 {
207     regs->al = inb_cmos(CMOS_MEM_EXTMEM_LOW);
208     regs->ah = inb_cmos(CMOS_MEM_EXTMEM_HIGH);
209     // According to Ralf Brown's interrupt the limit should be 15M,
210     // but real machines mostly return max. 63M.
211     if (regs->ax > 0xffc0)
212         regs->ax = 0xffc0;
213     set_success(regs);
214 }
215
216 // Device busy interrupt.  Called by Int 16h when no key available
217 static void
218 handle_1590(struct bregs *regs)
219 {
220 }
221
222 // Interrupt complete.  Called by Int 16h when key becomes available
223 static void
224 handle_1591(struct bregs *regs)
225 {
226 }
227
228 // keyboard intercept
229 static void
230 handle_154f(struct bregs *regs)
231 {
232     // set_fail(regs);  -- don't report this failure.
233     set_cf(regs, 1);
234 }
235
236 static void
237 handle_15c0(struct bregs *regs)
238 {
239     regs->es = SEG_BIOS;
240     regs->bx = (u16)&BIOS_CONFIG_TABLE;
241     set_code_success(regs);
242 }
243
244 static void
245 handle_15c1(struct bregs *regs)
246 {
247     regs->es = GET_BDA(ebda_seg);
248     set_success(regs);
249 }
250
251 static void
252 handle_15e801(struct bregs *regs)
253 {
254     // my real system sets ax and bx to 0
255     // this is confirmed by Ralph Brown list
256     // but syslinux v1.48 is known to behave
257     // strangely if ax is set to 0
258     // regs.u.r16.ax = 0;
259     // regs.u.r16.bx = 0;
260
261     // Get the amount of extended memory (above 1M)
262     regs->cl = inb_cmos(CMOS_MEM_EXTMEM_LOW);
263     regs->ch = inb_cmos(CMOS_MEM_EXTMEM_HIGH);
264
265     // limit to 15M
266     if (regs->cx > 0x3c00)
267         regs->cx = 0x3c00;
268
269     // Get the amount of extended memory above 16M in 64k blocs
270     regs->dl = inb_cmos(CMOS_MEM_EXTMEM2_LOW);
271     regs->dh = inb_cmos(CMOS_MEM_EXTMEM2_HIGH);
272
273     // Set configured memory equal to extended memory
274     regs->ax = regs->cx;
275     regs->bx = regs->dx;
276
277     set_success(regs);
278 }
279
280 #define ACPI_DATA_SIZE    0x00010000L
281
282 static void
283 set_e820_range(struct bregs *regs, u32 start, u32 end, u16 type, int last)
284 {
285     SET_FARVAR(regs->es, *(u16*)(regs->di+0), start);
286     SET_FARVAR(regs->es, *(u16*)(regs->di+2), start >> 16);
287     SET_FARVAR(regs->es, *(u16*)(regs->di+4), 0x00);
288     SET_FARVAR(regs->es, *(u16*)(regs->di+6), 0x00);
289
290     end -= start;
291     SET_FARVAR(regs->es, *(u16*)(regs->di+8), end);
292     SET_FARVAR(regs->es, *(u16*)(regs->di+10), end >> 16);
293     SET_FARVAR(regs->es, *(u16*)(regs->di+12), 0x0000);
294     SET_FARVAR(regs->es, *(u16*)(regs->di+14), 0x0000);
295
296     SET_FARVAR(regs->es, *(u16*)(regs->di+16), type);
297     SET_FARVAR(regs->es, *(u16*)(regs->di+18), 0x0);
298
299     if (last)
300         regs->ebx = 0;
301     else
302         regs->ebx++;
303     regs->eax = 0x534D4150;
304     regs->ecx = 0x14;
305     set_success(regs);
306 }
307
308 // XXX - should create e820 memory map in post and just copy it here.
309 static void
310 handle_15e820(struct bregs *regs)
311 {
312     if (regs->edx != 0x534D4150) {
313         set_code_fail(regs, RET_EUNSUPPORTED);
314         return;
315     }
316
317     u32 extended_memory_size = inb_cmos(CMOS_MEM_EXTMEM2_HIGH);
318     extended_memory_size <<= 8;
319     extended_memory_size |= inb_cmos(CMOS_MEM_EXTMEM2_LOW);
320     extended_memory_size *= 64;
321     // greater than EFF00000???
322     if (extended_memory_size > 0x3bc000)
323         // everything after this is reserved memory until we get to 0x100000000
324         extended_memory_size = 0x3bc000;
325     extended_memory_size *= 1024;
326     extended_memory_size += (16L * 1024 * 1024);
327
328     if (extended_memory_size <= (16L * 1024 * 1024)) {
329         extended_memory_size = inb_cmos(CMOS_MEM_EXTMEM_HIGH);
330         extended_memory_size <<= 8;
331         extended_memory_size |= inb_cmos(CMOS_MEM_EXTMEM_LOW);
332         extended_memory_size *= 1024;
333     }
334
335     switch (regs->bx) {
336     case 0:
337         set_e820_range(regs, 0x0000000L, 0x0009fc00L, E820_RAM, 0);
338         break;
339     case 1:
340         set_e820_range(regs, 0x0009fc00L, 0x000a0000L, E820_RESERVED, 0);
341         break;
342     case 2:
343         set_e820_range(regs, 0x000e8000L, 0x00100000L, E820_RESERVED, 0);
344         break;
345     case 3:
346         set_e820_range(regs, 0x00100000L,
347                        extended_memory_size - ACPI_DATA_SIZE, E820_RAM, 0);
348         break;
349     case 4:
350         set_e820_range(regs,
351                        extended_memory_size - ACPI_DATA_SIZE,
352                        extended_memory_size, E820_ACPI, 0);
353         break;
354     case 5:
355         /* 256KB BIOS area at the end of 4 GB */
356         set_e820_range(regs, 0xfffc0000L, 0x00000000L, E820_RESERVED, 1);
357         break;
358     default:  /* AX=E820, DX=534D4150, BX unrecognized */
359         set_code_fail(regs, RET_EUNSUPPORTED);
360     }
361 }
362
363 static void
364 handle_15e8XX(struct bregs *regs)
365 {
366     set_code_fail(regs, RET_EUNSUPPORTED);
367 }
368
369 static void
370 handle_15e8(struct bregs *regs)
371 {
372     switch (regs->al) {
373     case 0x01: handle_15e801(regs); break;
374     case 0x20: handle_15e820(regs); break;
375     default:   handle_15e8XX(regs); break;
376     }
377 }
378
379 static void
380 handle_15XX(struct bregs *regs)
381 {
382     set_code_fail(regs, RET_EUNSUPPORTED);
383 }
384
385 // INT 15h System Services Entry Point
386 void VISIBLE16
387 handle_15(struct bregs *regs)
388 {
389     //debug_enter(regs);
390     switch (regs->ah) {
391     case 0x24: handle_1524(regs); break;
392     case 0x4f: handle_154f(regs); break;
393     case 0x52: handle_1552(regs); break;
394     case 0x53: handle_1553(regs); break;
395     case 0x83: handle_1583(regs); break;
396     case 0x86: handle_1586(regs); break;
397     case 0x87: handle_1587(regs); break;
398     case 0x88: handle_1588(regs); break;
399     case 0x90: handle_1590(regs); break;
400     case 0x91: handle_1591(regs); break;
401     case 0xc0: handle_15c0(regs); break;
402     case 0xc1: handle_15c1(regs); break;
403     case 0xc2: handle_15c2(regs); break;
404     case 0xe8: handle_15e8(regs); break;
405     default:   handle_15XX(regs); break;
406     }
407 }
408
409 // INT 12h Memory Size Service Entry Point
410 void VISIBLE16
411 handle_12(struct bregs *regs)
412 {
413     debug_enter(regs);
414     regs->ax = GET_BDA(mem_size_kb);
415 }
416
417 // INT 11h Equipment List Service Entry Point
418 void VISIBLE16
419 handle_11(struct bregs *regs)
420 {
421     debug_enter(regs);
422     regs->ax = GET_BDA(equipment_list_flags);
423 }
424
425 // INT 05h Print Screen Service Entry Point
426 void VISIBLE16
427 handle_05(struct bregs *regs)
428 {
429     debug_enter(regs);
430 }
431
432 // INT 10h Video Support Service Entry Point
433 void VISIBLE16
434 handle_10(struct bregs *regs)
435 {
436     debug_enter(regs);
437     // dont do anything, since the VGA BIOS handles int10h requests
438 }
439
440 void VISIBLE16
441 handle_nmi(struct bregs *regs)
442 {
443     debug_isr(regs);
444     // XXX
445 }
446
447 // INT 75 - IRQ13 - MATH COPROCESSOR EXCEPTION
448 void VISIBLE16
449 handle_75(struct bregs *regs)
450 {
451     debug_isr(regs);
452
453     // clear irq13
454     outb(0, PORT_MATH_CLEAR);
455     // clear interrupt
456     eoi_both_pics();
457     // legacy nmi call
458     struct bregs br;
459     memset(&br, 0, sizeof(br));
460     call16_int(0x02, &br);
461 }