blob: 5be827c66b124d1d0520a747599a9b19b89d32c9 [file] [log] [blame]
wdenk2262cfe2002-11-18 00:14:45 +00001/*
2 * (C) Copyright 2002
Graeme Russdbf71152011-04-13 19:43:26 +10003 * 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
wdenk2262cfe2002-11-18 00:14:45 +000029#define REALMODE_MAILBOX ((char*)0xe00)
30
Graeme Russ067f9b12010-10-07 20:03:31 +110031extern ulong __realmode_start;
32extern ulong __realmode_size;
wdenk2262cfe2002-11-18 00:14:45 +000033extern char realmode_enter;
34
wdenk7a8e9bed2003-05-31 18:35:21 +000035int realmode_setup(void)
36{
Graeme Russ067f9b12010-10-07 20:03:31 +110037 ulong realmode_start = (ulong)&__realmode_start + gd->reloc_off;
38 ulong realmode_size = (ulong)&__realmode_size;
Graeme Russcabe5792009-11-24 20:04:20 +110039
wdenk7a8e9bed2003-05-31 18:35:21 +000040 /* copy the realmode switch code */
Graeme Russ3a25e942011-02-12 15:11:24 +110041 if (realmode_size > (REALMODE_MAILBOX - (char *)REALMODE_BASE)) {
wdenk8bde7f72003-06-27 21:31:46 +000042 printf("realmode switch too large (%ld bytes, max is %d)\n",
Graeme Russ3a25e942011-02-12 15:11:24 +110043 realmode_size,
44 (REALMODE_MAILBOX - (char *)REALMODE_BASE));
wdenk7a8e9bed2003-05-31 18:35:21 +000045 return -1;
46 }
wdenk8bde7f72003-06-27 21:31:46 +000047
Graeme Russ3a25e942011-02-12 15:11:24 +110048 memcpy((char *)REALMODE_BASE, (void *)realmode_start, realmode_size);
wdenk7a8e9bed2003-05-31 18:35:21 +000049 asm("wbinvd\n");
wdenk8bde7f72003-06-27 21:31:46 +000050
wdenk7a8e9bed2003-05-31 18:35:21 +000051 return 0;
52}
wdenk8bde7f72003-06-27 21:31:46 +000053
wdenk2262cfe2002-11-18 00:14:45 +000054int enter_realmode(u16 seg, u16 off, struct pt_regs *in, struct pt_regs *out)
55{
wdenk8bde7f72003-06-27 21:31:46 +000056
wdenk2262cfe2002-11-18 00:14:45 +000057 /* setup out thin bios emulation */
Graeme Russdbf71152011-04-13 19:43:26 +100058 if (bios_setup())
wdenk2262cfe2002-11-18 00:14:45 +000059 return -1;
wdenk8bde7f72003-06-27 21:31:46 +000060
Graeme Russdbf71152011-04-13 19:43:26 +100061 if (realmode_setup())
wdenk2262cfe2002-11-18 00:14:45 +000062 return -1;
wdenk8bde7f72003-06-27 21:31:46 +000063
wdenk2262cfe2002-11-18 00:14:45 +000064 in->eip = off;
65 in->xcs = seg;
wdenkea909b72002-11-21 23:11:29 +000066 if (3>(in->esp & 0xffff)) {
wdenk2262cfe2002-11-18 00:14:45 +000067 printf("Warning: entering realmode with sp < 4 will fail\n");
68 }
wdenk8bde7f72003-06-27 21:31:46 +000069
wdenk2262cfe2002-11-18 00:14:45 +000070 memcpy(REALMODE_MAILBOX, in, sizeof(struct pt_regs));
wdenk7a8e9bed2003-05-31 18:35:21 +000071 asm("wbinvd\n");
wdenk8bde7f72003-06-27 21:31:46 +000072
73 __asm__ volatile (
wdenk2262cfe2002-11-18 00:14:45 +000074 "lcall $0x20,%0\n" : : "i" (&realmode_enter) );
75
wdenk7a8e9bed2003-05-31 18:35:21 +000076 asm("wbinvd\n");
wdenk2262cfe2002-11-18 00:14:45 +000077 memcpy(out, REALMODE_MAILBOX, sizeof(struct pt_regs));
78
79 return out->eax;
80}
81
wdenk7a8e9bed2003-05-31 18:35:21 +000082
83/* This code is supposed to access a realmode interrupt
84 * it does currently not work for me */
85int enter_realmode_int(u8 lvl, struct pt_regs *in, struct pt_regs *out)
86{
87 /* place two instructions at 0x700 */
88 writeb(0xcd, 0x700); /* int $lvl */
89 writeb(lvl, 0x701);
90 writeb(0xcb, 0x702); /* lret */
91 asm("wbinvd\n");
wdenk8bde7f72003-06-27 21:31:46 +000092
93 enter_realmode(0x00, 0x700, in, out);
94
wdenk7a8e9bed2003-05-31 18:35:21 +000095 return out->eflags&1;
96}