wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Daniel Engström, Omicron Ceti AB, daniel@omicron.se |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 4 | * |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 5 | * 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> |
wdenk | 2029f4d | 2002-11-21 23:11:29 +0000 | [diff] [blame] | 27 | #include <asm/realmode.h> |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 28 | |
| 29 | |
| 30 | #define REALMODE_BASE ((char*)0x7c0) |
| 31 | #define REALMODE_MAILBOX ((char*)0xe00) |
| 32 | |
| 33 | |
Graeme Russ | ae428db | 2010-10-07 20:03:31 +1100 | [diff] [blame^] | 34 | extern ulong __realmode_start; |
| 35 | extern ulong __realmode_size; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 36 | extern char realmode_enter; |
| 37 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 38 | int realmode_setup(void) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 39 | { |
Graeme Russ | ae428db | 2010-10-07 20:03:31 +1100 | [diff] [blame^] | 40 | ulong realmode_start = (ulong)&__realmode_start + gd->reloc_off; |
| 41 | ulong realmode_size = (ulong)&__realmode_size; |
Graeme Russ | 759cbb2 | 2009-11-24 20:04:20 +1100 | [diff] [blame] | 42 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 43 | /* copy the realmode switch code */ |
Graeme Russ | ae428db | 2010-10-07 20:03:31 +1100 | [diff] [blame^] | 44 | if (realmode_size > (REALMODE_MAILBOX-REALMODE_BASE)) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 45 | printf("realmode switch too large (%ld bytes, max is %d)\n", |
Graeme Russ | ae428db | 2010-10-07 20:03:31 +1100 | [diff] [blame^] | 46 | realmode_size, (REALMODE_MAILBOX-REALMODE_BASE)); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 47 | return -1; |
| 48 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 49 | |
Graeme Russ | ae428db | 2010-10-07 20:03:31 +1100 | [diff] [blame^] | 50 | memcpy(REALMODE_BASE, (void*)realmode_start, realmode_size); |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 51 | asm("wbinvd\n"); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 52 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 53 | return 0; |
| 54 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 55 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 56 | int enter_realmode(u16 seg, u16 off, struct pt_regs *in, struct pt_regs *out) |
| 57 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 58 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 59 | /* setup out thin bios emulation */ |
| 60 | if (bios_setup()) { |
| 61 | return -1; |
| 62 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 63 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 64 | if (realmode_setup()) { |
| 65 | return -1; |
| 66 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 67 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 68 | in->eip = off; |
| 69 | in->xcs = seg; |
wdenk | 2029f4d | 2002-11-21 23:11:29 +0000 | [diff] [blame] | 70 | if (3>(in->esp & 0xffff)) { |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 71 | printf("Warning: entering realmode with sp < 4 will fail\n"); |
| 72 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 73 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 74 | memcpy(REALMODE_MAILBOX, in, sizeof(struct pt_regs)); |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 75 | asm("wbinvd\n"); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 76 | |
| 77 | __asm__ volatile ( |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 78 | "lcall $0x20,%0\n" : : "i" (&realmode_enter) ); |
| 79 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 80 | asm("wbinvd\n"); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 81 | memcpy(out, REALMODE_MAILBOX, sizeof(struct pt_regs)); |
| 82 | |
| 83 | return out->eax; |
| 84 | } |
| 85 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 86 | |
| 87 | /* This code is supposed to access a realmode interrupt |
| 88 | * it does currently not work for me */ |
| 89 | int enter_realmode_int(u8 lvl, struct pt_regs *in, struct pt_regs *out) |
| 90 | { |
| 91 | /* place two instructions at 0x700 */ |
| 92 | writeb(0xcd, 0x700); /* int $lvl */ |
| 93 | writeb(lvl, 0x701); |
| 94 | writeb(0xcb, 0x702); /* lret */ |
| 95 | asm("wbinvd\n"); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 96 | |
| 97 | enter_realmode(0x00, 0x700, in, out); |
| 98 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 99 | return out->eflags&1; |
| 100 | } |