1 // Handler for int 0x15 "system" calls
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "biosvar.h" // GET_GLOBAL
9 #include "bregs.h" // struct bregs
10 #include "e820map.h" // E820_RAM
11 #include "hw/pic.h" // pic_reset
12 #include "malloc.h" // LegacyRamSize
13 #include "output.h" // debug_enter
14 #include "string.h" // memcpy_far
15 #include "util.h" // handle_1553
16 #include "x86.h" // set_a20
19 handle_152400(struct bregs *regs)
22 set_code_success(regs);
26 handle_152401(struct bregs *regs)
29 set_code_success(regs);
33 handle_152402(struct bregs *regs)
36 set_code_success(regs);
40 handle_152403(struct bregs *regs)
43 set_code_success(regs);
47 handle_1524XX(struct bregs *regs)
49 set_code_unimplemented(regs, RET_EUNSUPPORTED);
53 handle_1524(struct bregs *regs)
56 case 0x00: handle_152400(regs); break;
57 case 0x01: handle_152401(regs); break;
58 case 0x02: handle_152402(regs); break;
59 case 0x03: handle_152403(regs); break;
60 default: handle_1524XX(regs); break;
64 // removable media eject
66 handle_1552(struct bregs *regs)
68 set_code_success(regs);
72 handle_1587(struct bregs *regs)
74 // +++ should probably have descriptor checks
75 // +++ should have exception handlers
77 u8 prev_a20_enable = set_a20(1); // enable A20 line
79 // 128K max of transfer on 386+ ???
80 // source == destination ???
82 // ES:SI points to descriptor table
83 // offset use initially comments
84 // ==============================================
85 // 00..07 Unused zeros Null descriptor
86 // 08..0f GDT zeros filled in by BIOS
87 // 10..17 source ssssssss source of data
88 // 18..1f dest dddddddd destination of data
89 // 20..27 CS zeros filled in by BIOS
90 // 28..2f SS zeros filled in by BIOS
92 // check for access rights of source & dest here
94 // Initialize GDT descriptor
95 u64 *gdt_far = (void*)(regs->si + 0);
96 u16 gdt_seg = regs->es;
97 u32 loc = (u32)MAKE_FLATPTR(gdt_seg, gdt_far);
98 SET_FARVAR(gdt_seg, gdt_far[1], GDT_DATA | GDT_LIMIT((6*sizeof(u64))-1)
100 // Initialize CS descriptor
101 u64 lim = GDT_LIMIT(0x0ffff);
103 lim = GDT_GRANLIMIT(0xffffffff);
104 SET_FARVAR(gdt_seg, gdt_far[4], GDT_CODE | lim | GDT_BASE(BUILD_BIOS_ADDR));
105 // Initialize SS descriptor
106 loc = (u32)MAKE_FLATPTR(GET_SEG(SS), 0);
107 SET_FARVAR(gdt_seg, gdt_far[5], GDT_DATA | lim | GDT_BASE(loc));
109 SET_SEG(ES, gdt_seg);
110 u16 count = regs->cx, si = 0, di = 0;
112 // Load new descriptor tables
113 " lgdtw %%es:(1<<3)(%%eax)\n"
114 " lidtw %%cs:pmode_IDT_info\n"
116 // Enable protected mode
117 " movl %%cr0, %%eax\n"
118 " orl $" __stringify(CR0_PE) ", %%eax\n"
119 " movl %%eax, %%cr0\n"
121 // far jump to flush CPU queue after transition to protected mode
122 " ljmpw $(4<<3), $1f\n"
124 // GDT points to valid descriptor table, now load DS, ES
125 "1:movw $(2<<3), %%ax\n" // 2nd descriptor in table, TI=GDT, RPL=00
127 " movw $(3<<3), %%ax\n" // 3rd descriptor in table, TI=GDT, RPL=00
130 // memcpy CX words using 32bit memcpy if applicable
134 " rep movsl %%ds:(%%si), %%es:(%%di)\n"
136 // Restore DS and ES segment limits to 0xffff
137 "2:movw $(5<<3), %%ax\n" // 5th descriptor in table (SS)
141 // Disable protected mode
142 " movl %%cr0, %%eax\n"
143 " andl $~" __stringify(CR0_PE) ", %%eax\n"
144 " movl %%eax, %%cr0\n"
146 // far jump to flush CPU queue after transition to real mode
147 " ljmpw $" __stringify(SEG_BIOS) ", $4f\n"
149 // Slower 16bit copy method
150 "3:rep movsw %%ds:(%%si), %%es:(%%di)\n"
153 // restore IDT to normal real-mode defaults
154 "4:lidtw %%cs:rmode_IDT_info\n"
156 // Restore %ds (from %ss)
159 : "+a" (gdt_far), "+c"(count), "+m" (__segment_ES)
163 set_a20(prev_a20_enable);
165 set_code_success(regs);
168 // Get the amount of extended memory (above 1M)
170 handle_1588(struct bregs *regs)
172 u32 rs = GET_GLOBAL(LegacyRamSize);
174 // According to Ralf Brown's interrupt the limit should be 15M,
175 // but real machines mostly return max. 63M.
176 if (rs > 64*1024*1024)
177 regs->ax = 63 * 1024;
179 regs->ax = (rs - 1*1024*1024) / 1024;
183 // Switch to protected mode
185 handle_1589(struct bregs *regs)
187 debug_enter(regs, DEBUG_HDL_15);
190 pic_reset(regs->bl, regs->bh);
192 u64 *gdt_far = (void*)(regs->si + 0);
193 u16 gdt_seg = regs->es;
194 SET_FARVAR(gdt_seg, gdt_far[7], GDT_CODE | GDT_LIMIT(BUILD_BIOS_SIZE-1)
195 | GDT_BASE(BUILD_BIOS_ADDR));
197 regs->ds = 3<<3; // 3rd gdt descriptor is %ds
198 regs->es = 4<<3; // 4th gdt descriptor is %es
199 regs->code.seg = 6<<3; // 6th gdt descriptor is %cs
201 set_code_success(regs);
204 // Load new descriptor tables
205 " lgdtw %%es:(1<<3)(%%si)\n"
206 " lidtw %%es:(2<<3)(%%si)\n"
208 // Enable protected mode
209 " movl %%cr0, %%eax\n"
210 " orl $" __stringify(CR0_PE) ", %%eax\n"
211 " movl %%eax, %%cr0\n"
213 // far jump to flush CPU queue after transition to protected mode
214 " ljmpw $(7<<3), $1f\n"
216 // GDT points to valid descriptor table, now load SS
217 "1:movw $(5<<3), %%ax\n"
221 : "S"(gdt_far), "m" (__segment_ES)
225 // Device busy interrupt. Called by Int 16h when no key available
227 handle_1590(struct bregs *regs)
231 // Interrupt complete. Called by Int 16h when key becomes available
233 handle_1591(struct bregs *regs)
237 // keyboard intercept
239 handle_154f(struct bregs *regs)
241 set_invalid_silent(regs);
245 handle_15c0(struct bregs *regs)
248 regs->bx = (u32)&BIOS_CONFIG_TABLE;
249 set_code_success(regs);
253 handle_15c1(struct bregs *regs)
255 regs->es = get_ebda_seg();
260 handle_15e801(struct bregs *regs)
262 // my real system sets ax and bx to 0
263 // this is confirmed by Ralph Brown list
264 // but syslinux v1.48 is known to behave
265 // strangely if ax is set to 0
266 // regs.u.r16.ax = 0;
267 // regs.u.r16.bx = 0;
269 u32 rs = GET_GLOBAL(LegacyRamSize);
271 // Get the amount of extended memory (above 1M)
272 if (rs > 16*1024*1024) {
275 // Get the amount of extended memory above 16M in 64k blocks
276 regs->dx = (rs - 16*1024*1024) / (64*1024);
278 regs->cx = (rs - 1*1024*1024) / 1024;
282 // Set configured memory equal to extended memory
290 handle_15e820(struct bregs *regs)
292 int count = GET_GLOBAL(e820_count);
293 if (regs->edx != 0x534D4150 || regs->bx >= count
294 || regs->ecx < sizeof(e820_list[0])) {
295 set_code_invalid(regs, RET_EUNSUPPORTED);
299 memcpy_far(regs->es, (void*)(regs->di+0)
300 , get_global_seg(), &e820_list[regs->bx]
301 , sizeof(e820_list[0]));
302 if (regs->bx == count-1)
306 regs->eax = 0x534D4150;
307 regs->ecx = sizeof(e820_list[0]);
312 handle_15e8XX(struct bregs *regs)
314 set_code_unimplemented(regs, RET_EUNSUPPORTED);
318 handle_15e8(struct bregs *regs)
321 case 0x01: handle_15e801(regs); break;
322 case 0x20: handle_15e820(regs); break;
323 default: handle_15e8XX(regs); break;
328 handle_15XX(struct bregs *regs)
330 set_code_unimplemented(regs, RET_EUNSUPPORTED);
333 // INT 15h System Services Entry Point
335 handle_15(struct bregs *regs)
337 debug_enter(regs, DEBUG_HDL_15);
339 case 0x24: handle_1524(regs); break;
340 case 0x4f: handle_154f(regs); break;
341 case 0x52: handle_1552(regs); break;
342 case 0x53: handle_1553(regs); break;
343 case 0x5f: handle_155f(regs); break;
344 case 0x7f: handle_157f(regs); break;
345 case 0x83: handle_1583(regs); break;
346 case 0x86: handle_1586(regs); break;
347 case 0x87: handle_1587(regs); break;
348 case 0x88: handle_1588(regs); break;
349 case 0x90: handle_1590(regs); break;
350 case 0x91: handle_1591(regs); break;
351 case 0xc0: handle_15c0(regs); break;
352 case 0xc1: handle_15c1(regs); break;
353 case 0xc2: handle_15c2(regs); break;
354 case 0xe8: handle_15e8(regs); break;
355 default: handle_15XX(regs); break;