blob: 75511b2bdf9893f29483d6d3b70d8817f43c3f47 [file] [log] [blame]
wdenk591dda52002-11-18 00:14:45 +00001/*
2 * (C) Copyright 2002
Albert ARIBAUD60fbc8d2011-08-04 18:45:45 +02003 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
wdenk57b2d802003-06-27 21:31:46 +00004 *
wdenk591dda52002-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>
wdenk2029f4d2002-11-21 23:11:29 +000027#include <asm/realmode.h>
wdenk591dda52002-11-18 00:14:45 +000028
Graeme Russ883c6032011-11-08 02:33:15 +000029#define REALMODE_MAILBOX ((char *)0xe00)
wdenk591dda52002-11-18 00:14:45 +000030
wdenkabda5ca2003-05-31 18:35:21 +000031int realmode_setup(void)
wdenk591dda52002-11-18 00:14:45 +000032{
Gabe Black3530c072011-11-12 16:34:48 +000033 /* The realmode section is not relocated and still in the ROM. */
34 ulong realmode_start = (ulong)&__realmode_start;
Graeme Russae428db2010-10-07 20:03:31 +110035 ulong realmode_size = (ulong)&__realmode_size;
Graeme Russ759cbb22009-11-24 20:04:20 +110036
wdenk591dda52002-11-18 00:14:45 +000037 /* copy the realmode switch code */
Graeme Russ3fe48602011-02-12 15:11:24 +110038 if (realmode_size > (REALMODE_MAILBOX - (char *)REALMODE_BASE)) {
wdenk57b2d802003-06-27 21:31:46 +000039 printf("realmode switch too large (%ld bytes, max is %d)\n",
Graeme Russ3fe48602011-02-12 15:11:24 +110040 realmode_size,
Gabe Black46207f42011-10-20 18:29:17 +000041 (int)(REALMODE_MAILBOX - (char *)REALMODE_BASE));
wdenk591dda52002-11-18 00:14:45 +000042 return -1;
43 }
wdenk57b2d802003-06-27 21:31:46 +000044
Graeme Russ3fe48602011-02-12 15:11:24 +110045 memcpy((char *)REALMODE_BASE, (void *)realmode_start, realmode_size);
wdenkabda5ca2003-05-31 18:35:21 +000046 asm("wbinvd\n");
wdenk57b2d802003-06-27 21:31:46 +000047
wdenkabda5ca2003-05-31 18:35:21 +000048 return 0;
49}
wdenk57b2d802003-06-27 21:31:46 +000050
wdenkabda5ca2003-05-31 18:35:21 +000051int enter_realmode(u16 seg, u16 off, struct pt_regs *in, struct pt_regs *out)
52{
wdenk57b2d802003-06-27 21:31:46 +000053
wdenkabda5ca2003-05-31 18:35:21 +000054 /* setup out thin bios emulation */
Graeme Russ45fc1d82011-04-13 19:43:26 +100055 if (bios_setup())
wdenkabda5ca2003-05-31 18:35:21 +000056 return -1;
wdenk57b2d802003-06-27 21:31:46 +000057
Graeme Russ45fc1d82011-04-13 19:43:26 +100058 if (realmode_setup())
wdenkabda5ca2003-05-31 18:35:21 +000059 return -1;
wdenk57b2d802003-06-27 21:31:46 +000060
wdenk591dda52002-11-18 00:14:45 +000061 in->eip = off;
62 in->xcs = seg;
Graeme Russ883c6032011-11-08 02:33:15 +000063 if ((in->esp & 0xffff) < 4)
wdenk591dda52002-11-18 00:14:45 +000064 printf("Warning: entering realmode with sp < 4 will fail\n");
wdenk57b2d802003-06-27 21:31:46 +000065
wdenk591dda52002-11-18 00:14:45 +000066 memcpy(REALMODE_MAILBOX, in, sizeof(struct pt_regs));
wdenkabda5ca2003-05-31 18:35:21 +000067 asm("wbinvd\n");
wdenk57b2d802003-06-27 21:31:46 +000068
69 __asm__ volatile (
Graeme Russ883c6032011-11-08 02:33:15 +000070 "lcall $0x20,%0\n" : : "i" (&realmode_enter));
wdenk591dda52002-11-18 00:14:45 +000071
wdenkabda5ca2003-05-31 18:35:21 +000072 asm("wbinvd\n");
wdenk591dda52002-11-18 00:14:45 +000073 memcpy(out, REALMODE_MAILBOX, sizeof(struct pt_regs));
74
75 return out->eax;
76}
77
Graeme Russ883c6032011-11-08 02:33:15 +000078/*
79 * This code is supposed to access a realmode interrupt
80 * it does currently not work for me
81 */
wdenkabda5ca2003-05-31 18:35:21 +000082int enter_realmode_int(u8 lvl, struct pt_regs *in, struct pt_regs *out)
83{
84 /* place two instructions at 0x700 */
85 writeb(0xcd, 0x700); /* int $lvl */
86 writeb(lvl, 0x701);
87 writeb(0xcb, 0x702); /* lret */
88 asm("wbinvd\n");
wdenk57b2d802003-06-27 21:31:46 +000089
90 enter_realmode(0x00, 0x700, in, out);
91
Graeme Russ883c6032011-11-08 02:33:15 +000092 return out->eflags & 0x00000001;
wdenkabda5ca2003-05-31 18:35:21 +000093}