blob: ebb8d41d4df060a95bd26aa6a2d527a7d3d7e77f [file] [log] [blame]
Kevin O'Connor30853762009-01-17 18:49:20 -05001// Handler for int 0x15 "system" calls
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05002//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05007
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'Connorf54c1502008-06-14 15:56:16 -040012#include "pic.h" // eoi_pic2
Kevin O'Connor9521e262008-07-04 13:04:29 -040013#include "bregs.h" // struct bregs
Kevin O'Connore2e5f012008-03-08 10:27:39 -050014
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050015// Use PS2 System Control port A to set A20 enable
16static inline u8
17set_a20(u8 cond)
18{
19 // get current setting first
20 u8 newval, oldval = inb(PORT_A20);
21 if (cond)
Kevin O'Connord9a8b2d2008-11-16 09:17:02 -050022 newval = oldval | A20_ENABLE_BIT;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050023 else
Kevin O'Connord9a8b2d2008-11-16 09:17:02 -050024 newval = oldval & ~A20_ENABLE_BIT;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050025 outb(newval, PORT_A20);
26
Kevin O'Connord9a8b2d2008-11-16 09:17:02 -050027 return (oldval & A20_ENABLE_BIT) != 0;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050028}
29
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050030static void
31handle_152400(struct bregs *regs)
32{
33 set_a20(0);
Kevin O'Connor6c781222008-03-09 12:19:23 -040034 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050035}
36
37static void
38handle_152401(struct bregs *regs)
39{
40 set_a20(1);
Kevin O'Connor6c781222008-03-09 12:19:23 -040041 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050042}
43
44static void
45handle_152402(struct bregs *regs)
46{
Kevin O'Connord9a8b2d2008-11-16 09:17:02 -050047 regs->al = (inb(PORT_A20) & A20_ENABLE_BIT) != 0;
Kevin O'Connor6c781222008-03-09 12:19:23 -040048 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050049}
50
51static void
52handle_152403(struct bregs *regs)
53{
54 regs->bx = 3;
Kevin O'Connor6c781222008-03-09 12:19:23 -040055 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050056}
57
58static void
59handle_1524XX(struct bregs *regs)
60{
Kevin O'Connor6c781222008-03-09 12:19:23 -040061 set_code_fail(regs, RET_EUNSUPPORTED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050062}
63
Kevin O'Connoradb6b372008-03-01 13:38:38 -050064static void
65handle_1524(struct bregs *regs)
66{
67 switch (regs->al) {
68 case 0x00: handle_152400(regs); break;
69 case 0x01: handle_152401(regs); break;
70 case 0x02: handle_152402(regs); break;
71 case 0x03: handle_152403(regs); break;
72 default: handle_1524XX(regs); break;
73 }
74}
75
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050076// removable media eject
77static void
78handle_1552(struct bregs *regs)
79{
Kevin O'Connor6c781222008-03-09 12:19:23 -040080 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050081}
82
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050083static void
84handle_1587(struct bregs *regs)
85{
86 // +++ should probably have descriptor checks
87 // +++ should have exception handlers
88
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050089 u8 prev_a20_enable = set_a20(1); // enable A20 line
90
91 // 128K max of transfer on 386+ ???
92 // source == destination ???
93
94 // ES:SI points to descriptor table
95 // offset use initially comments
96 // ==============================================
97 // 00..07 Unused zeros Null descriptor
98 // 08..0f GDT zeros filled in by BIOS
99 // 10..17 source ssssssss source of data
100 // 18..1f dest dddddddd destination of data
101 // 20..27 CS zeros filled in by BIOS
102 // 28..2f SS zeros filled in by BIOS
103
104 //es:si
105 //eeee0
106 //0ssss
107 //-----
108
109// check for access rights of source & dest here
110
111 // Initialize GDT descriptor
Kevin O'Connor67c00dd2008-03-09 16:10:19 -0400112 SET_SEG(ES, regs->es);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500113 u16 si = regs->si;
114 u16 base15_00 = (regs->es << 4) + si;
115 u16 base23_16 = regs->es >> 12;
Kevin O'Connor67c00dd2008-03-09 16:10:19 -0400116 if (base15_00 < (u16)(regs->es<<4))
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500117 base23_16++;
118 SET_VAR(ES, *(u16*)(si+0x08+0), 47); // limit 15:00 = 6 * 8bytes/descriptor
119 SET_VAR(ES, *(u16*)(si+0x08+2), base15_00);// base 15:00
120 SET_VAR(ES, *(u8 *)(si+0x08+4), base23_16);// base 23:16
121 SET_VAR(ES, *(u8 *)(si+0x08+5), 0x93); // access
122 SET_VAR(ES, *(u16*)(si+0x08+6), 0x0000); // base 31:24/reserved/limit 19:16
123
124 // Initialize CS descriptor
125 SET_VAR(ES, *(u16*)(si+0x20+0), 0xffff);// limit 15:00 = normal 64K limit
126 SET_VAR(ES, *(u16*)(si+0x20+2), 0x0000);// base 15:00
127 SET_VAR(ES, *(u8 *)(si+0x20+4), 0x000f);// base 23:16
128 SET_VAR(ES, *(u8 *)(si+0x20+5), 0x9b); // access
129 SET_VAR(ES, *(u16*)(si+0x20+6), 0x0000);// base 31:24/reserved/limit 19:16
130
131 // Initialize SS descriptor
132 u16 ss = GET_SEG(SS);
133 base15_00 = ss << 4;
134 base23_16 = ss >> 12;
135 SET_VAR(ES, *(u16*)(si+0x28+0), 0xffff); // limit 15:00 = normal 64K limit
136 SET_VAR(ES, *(u16*)(si+0x28+2), base15_00);// base 15:00
137 SET_VAR(ES, *(u8 *)(si+0x28+4), base23_16);// base 23:16
138 SET_VAR(ES, *(u8 *)(si+0x28+5), 0x93); // access
139 SET_VAR(ES, *(u16*)(si+0x28+6), 0x0000); // base 31:24/reserved/limit 19:16
140
Kevin O'Connor2cdd8b62008-03-09 23:37:04 -0400141 u16 count = regs->cx;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500142 asm volatile(
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500143 // Load new descriptor tables
Kevin O'Connor67c00dd2008-03-09 16:10:19 -0400144 "lgdtw %%es:0x8(%%si)\n"
145 "lidtw %%cs:pmode_IDT_info\n"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500146
Kevin O'Connor3eac0092008-11-16 09:59:32 -0500147 // Enable protected mode
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500148 "movl %%cr0, %%eax\n"
Kevin O'Connor3eac0092008-11-16 09:59:32 -0500149 "orl $" __stringify(CR0_PE) ", %%eax\n"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500150 "movl %%eax, %%cr0\n"
151
152 // far jump to flush CPU queue after transition to protected mode
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500153 "ljmpw $0x0020, $1f\n"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500154 "1:\n"
155
156 // GDT points to valid descriptor table, now load DS, ES
157 "movw $0x10, %%ax\n" // 010 000 = 2nd descriptor in table, TI=GDT, RPL=00
158 "movw %%ax, %%ds\n"
159 "movw $0x18, %%ax\n" // 011 000 = 3rd descriptor in table, TI=GDT, RPL=00
160 "movw %%ax, %%es\n"
161
162 // move CX words from DS:SI to ES:DI
163 "xorw %%si, %%si\n"
164 "xorw %%di, %%di\n"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500165 "rep movsw\n"
166
Kevin O'Connor3eac0092008-11-16 09:59:32 -0500167 // Disable protected mode
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500168 "movl %%cr0, %%eax\n"
Kevin O'Connor3eac0092008-11-16 09:59:32 -0500169 "andl $~" __stringify(CR0_PE) ", %%eax\n"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500170 "movl %%eax, %%cr0\n"
171
172 // far jump to flush CPU queue after transition to real mode
Kevin O'Connore3677b12008-07-04 15:29:23 -0400173 "ljmpw $" __stringify(SEG_BIOS) ", $2f\n"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500174 "2:\n"
175
176 // restore IDT to normal real-mode defaults
Kevin O'Connor9649a962008-12-10 20:53:35 -0500177 "lidtw %%cs:rmode_IDT_info\n"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500178
Kevin O'Connore20ed9f2008-03-01 14:25:44 -0500179 // Restore %ds (from %ss)
180 "movw %%ss, %%ax\n"
181 "movw %%ax, %%ds\n"
Kevin O'Connor2cdd8b62008-03-09 23:37:04 -0400182 : "+c"(count), "+S"(si)
Kevin O'Connored128492008-03-11 11:14:59 -0400183 : : "eax", "di", "cc"); // XXX - also clobbers %es
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500184
185 set_a20(prev_a20_enable);
186
Kevin O'Connor6c781222008-03-09 12:19:23 -0400187 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500188}
189
190// Get the amount of extended memory (above 1M)
191static void
192handle_1588(struct bregs *regs)
193{
Kevin O'Connore7916362008-12-28 22:03:17 -0500194 u32 rs = GET_GLOBAL(RamSize);
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400195
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500196 // According to Ralf Brown's interrupt the limit should be 15M,
197 // but real machines mostly return max. 63M.
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400198 if (rs > 64*1024*1024)
199 regs->ax = 63 * 1024;
200 else
201 regs->ax = (rs - 1*1024*1024) / 1024;
Kevin O'Connor6c781222008-03-09 12:19:23 -0400202 set_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500203}
204
205// Device busy interrupt. Called by Int 16h when no key available
206static void
207handle_1590(struct bregs *regs)
208{
209}
210
211// Interrupt complete. Called by Int 16h when key becomes available
212static void
213handle_1591(struct bregs *regs)
214{
215}
216
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500217// keyboard intercept
218static void
219handle_154f(struct bregs *regs)
220{
Kevin O'Connordb9e65e2008-06-07 14:41:21 -0400221 set_fail_silent(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500222}
223
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500224static void
225handle_15c0(struct bregs *regs)
226{
227 regs->es = SEG_BIOS;
Kevin O'Connor117fc212008-04-13 18:17:02 -0400228 regs->bx = (u32)&BIOS_CONFIG_TABLE;
Kevin O'Connor6c781222008-03-09 12:19:23 -0400229 set_code_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500230}
231
232static void
233handle_15c1(struct bregs *regs)
234{
Kevin O'Connor08815372008-12-29 21:16:31 -0500235 regs->es = get_ebda_seg();
Kevin O'Connor6c781222008-03-09 12:19:23 -0400236 set_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500237}
238
239static void
240handle_15e801(struct bregs *regs)
241{
242 // my real system sets ax and bx to 0
243 // this is confirmed by Ralph Brown list
244 // but syslinux v1.48 is known to behave
245 // strangely if ax is set to 0
246 // regs.u.r16.ax = 0;
247 // regs.u.r16.bx = 0;
248
Kevin O'Connore7916362008-12-28 22:03:17 -0500249 u32 rs = GET_GLOBAL(RamSize);
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400250
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500251 // Get the amount of extended memory (above 1M)
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400252 if (rs > 16*1024*1024) {
253 // limit to 15M
254 regs->cx = 15*1024;
255 // Get the amount of extended memory above 16M in 64k blocks
256 regs->dx = (rs - 16*1024*1024) / (64*1024);
257 } else {
258 regs->cx = (rs - 1*1024*1024) / 1024;
259 regs->dx = 0;
260 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500261
262 // Set configured memory equal to extended memory
263 regs->ax = regs->cx;
264 regs->bx = regs->dx;
265
Kevin O'Connor6c781222008-03-09 12:19:23 -0400266 set_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500267}
268
Kevin O'Connord995b3d2008-11-08 13:05:27 -0500269// Info on e820 map location and size.
Kevin O'Connor92f95b02008-12-29 20:42:40 -0500270struct e820entry *e820_list VAR16_32;
271int e820_count VAR16_32;
Kevin O'Connord995b3d2008-11-08 13:05:27 -0500272
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500273static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500274handle_15e820(struct bregs *regs)
275{
Kevin O'Connor15157a32008-12-13 11:10:37 -0500276 int count = GET_GLOBAL(e820_count);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400277 if (regs->edx != 0x534D4150 || regs->bx >= count) {
Kevin O'Connor6c781222008-03-09 12:19:23 -0400278 set_code_fail(regs, RET_EUNSUPPORTED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500279 return;
280 }
281
Kevin O'Connor15157a32008-12-13 11:10:37 -0500282 struct e820entry *l = GET_GLOBAL(e820_list);
Kevin O'Connord995b3d2008-11-08 13:05:27 -0500283 memcpy_far(MAKE_FARPTR(regs->es, regs->di), &l[regs->bx], sizeof(l[0]));
Kevin O'Connorc7812932008-06-08 23:08:12 -0400284 if (regs->bx == count-1)
285 regs->ebx = 0;
286 else
287 regs->ebx++;
288 regs->eax = 0x534D4150;
Kevin O'Connord995b3d2008-11-08 13:05:27 -0500289 regs->ecx = sizeof(l[0]);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400290 set_success(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500291}
292
293static void
294handle_15e8XX(struct bregs *regs)
295{
Kevin O'Connor6c781222008-03-09 12:19:23 -0400296 set_code_fail(regs, RET_EUNSUPPORTED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500297}
298
299static void
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500300handle_15e8(struct bregs *regs)
301{
302 switch (regs->al) {
303 case 0x01: handle_15e801(regs); break;
304 case 0x20: handle_15e820(regs); break;
305 default: handle_15e8XX(regs); break;
306 }
307}
308
309static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500310handle_15XX(struct bregs *regs)
311{
Kevin O'Connor6c781222008-03-09 12:19:23 -0400312 set_code_fail(regs, RET_EUNSUPPORTED);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500313}
314
315// INT 15h System Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500316void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500317handle_15(struct bregs *regs)
318{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400319 debug_enter(regs, DEBUG_HDL_15);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500320 switch (regs->ah) {
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500321 case 0x24: handle_1524(regs); break;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500322 case 0x4f: handle_154f(regs); break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500323 case 0x52: handle_1552(regs); break;
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500324 case 0x53: handle_1553(regs); break;
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400325 case 0x5f: handle_155f(regs); break;
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500326 case 0x83: handle_1583(regs); break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500327 case 0x86: handle_1586(regs); break;
328 case 0x87: handle_1587(regs); break;
329 case 0x88: handle_1588(regs); break;
330 case 0x90: handle_1590(regs); break;
331 case 0x91: handle_1591(regs); break;
332 case 0xc0: handle_15c0(regs); break;
333 case 0xc1: handle_15c1(regs); break;
334 case 0xc2: handle_15c2(regs); break;
Kevin O'Connoradb6b372008-03-01 13:38:38 -0500335 case 0xe8: handle_15e8(regs); break;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500336 default: handle_15XX(regs); break;
337 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500338}