blob: 6cf273885b838ed55d1c2d02710a722ee4bc0f98 [file] [log] [blame]
wdenk2262cfe2002-11-18 00:14:45 +00001/*
2 * (C) Copyright 2002
3 * Daniel Engström, Omicron Ceti AB, daniel@omicron.se
wdenk8bde7f72003-06-27 21:31:46 +00004 *
wdenk2262cfe2002-11-18 00:14:45 +00005 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <asm/io.h>
26#include <asm/ptrace.h>
wdenkea909b72002-11-21 23:11:29 +000027#include <asm/realmode.h>
wdenk2262cfe2002-11-18 00:14:45 +000028
29
30#define REALMODE_BASE ((char*)0x7c0)
31#define REALMODE_MAILBOX ((char*)0xe00)
32
33
34extern char realmode_enter;
35
wdenk7a8e9bed2003-05-31 18:35:21 +000036int realmode_setup(void)
37{
38 /* copy the realmode switch code */
39 if (i386boot_realmode_size > (REALMODE_MAILBOX-REALMODE_BASE)) {
wdenk8bde7f72003-06-27 21:31:46 +000040 printf("realmode switch too large (%ld bytes, max is %d)\n",
wdenk7a8e9bed2003-05-31 18:35:21 +000041 i386boot_realmode_size, (REALMODE_MAILBOX-REALMODE_BASE));
42 return -1;
43 }
wdenk8bde7f72003-06-27 21:31:46 +000044
wdenk7a8e9bed2003-05-31 18:35:21 +000045 memcpy(REALMODE_BASE, (void*)i386boot_realmode, i386boot_realmode_size);
46 asm("wbinvd\n");
wdenk8bde7f72003-06-27 21:31:46 +000047
wdenk7a8e9bed2003-05-31 18:35:21 +000048 return 0;
49}
wdenk8bde7f72003-06-27 21:31:46 +000050
wdenk2262cfe2002-11-18 00:14:45 +000051int enter_realmode(u16 seg, u16 off, struct pt_regs *in, struct pt_regs *out)
52{
wdenk8bde7f72003-06-27 21:31:46 +000053
wdenk2262cfe2002-11-18 00:14:45 +000054 /* setup out thin bios emulation */
55 if (bios_setup()) {
56 return -1;
57 }
wdenk8bde7f72003-06-27 21:31:46 +000058
wdenk7a8e9bed2003-05-31 18:35:21 +000059 if (realmode_setup()) {
wdenk2262cfe2002-11-18 00:14:45 +000060 return -1;
61 }
wdenk8bde7f72003-06-27 21:31:46 +000062
wdenk2262cfe2002-11-18 00:14:45 +000063 in->eip = off;
64 in->xcs = seg;
wdenkea909b72002-11-21 23:11:29 +000065 if (3>(in->esp & 0xffff)) {
wdenk2262cfe2002-11-18 00:14:45 +000066 printf("Warning: entering realmode with sp < 4 will fail\n");
67 }
wdenk8bde7f72003-06-27 21:31:46 +000068
wdenk2262cfe2002-11-18 00:14:45 +000069 memcpy(REALMODE_MAILBOX, in, sizeof(struct pt_regs));
wdenk7a8e9bed2003-05-31 18:35:21 +000070 asm("wbinvd\n");
wdenk8bde7f72003-06-27 21:31:46 +000071
72 __asm__ volatile (
wdenk2262cfe2002-11-18 00:14:45 +000073 "lcall $0x20,%0\n" : : "i" (&realmode_enter) );
74
wdenk7a8e9bed2003-05-31 18:35:21 +000075 asm("wbinvd\n");
wdenk2262cfe2002-11-18 00:14:45 +000076 memcpy(out, REALMODE_MAILBOX, sizeof(struct pt_regs));
77
78 return out->eax;
79}
80
wdenk7a8e9bed2003-05-31 18:35:21 +000081
82/* This code is supposed to access a realmode interrupt
83 * it does currently not work for me */
84int enter_realmode_int(u8 lvl, struct pt_regs *in, struct pt_regs *out)
85{
86 /* place two instructions at 0x700 */
87 writeb(0xcd, 0x700); /* int $lvl */
88 writeb(lvl, 0x701);
89 writeb(0xcb, 0x702); /* lret */
90 asm("wbinvd\n");
wdenk8bde7f72003-06-27 21:31:46 +000091
92 enter_realmode(0x00, 0x700, in, out);
93
wdenk7a8e9bed2003-05-31 18:35:21 +000094 return out->eflags&1;
95}