realmode.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * (C) Copyright 2002
  3. * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
  4. *
  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. #include <common.h>
  24. #include <asm/io.h>
  25. #include <asm/ptrace.h>
  26. #include <asm/realmode.h>
  27. #define REALMODE_MAILBOX ((char *)0xe00)
  28. int realmode_setup(void)
  29. {
  30. ulong realmode_start = (ulong)&__realmode_start + gd->reloc_off;
  31. ulong realmode_size = (ulong)&__realmode_size;
  32. /* copy the realmode switch code */
  33. if (realmode_size > (REALMODE_MAILBOX - (char *)REALMODE_BASE)) {
  34. printf("realmode switch too large (%ld bytes, max is %d)\n",
  35. realmode_size,
  36. (int)(REALMODE_MAILBOX - (char *)REALMODE_BASE));
  37. return -1;
  38. }
  39. memcpy((char *)REALMODE_BASE, (void *)realmode_start, realmode_size);
  40. asm("wbinvd\n");
  41. return 0;
  42. }
  43. int enter_realmode(u16 seg, u16 off, struct pt_regs *in, struct pt_regs *out)
  44. {
  45. /* setup out thin bios emulation */
  46. if (bios_setup())
  47. return -1;
  48. if (realmode_setup())
  49. return -1;
  50. in->eip = off;
  51. in->xcs = seg;
  52. if ((in->esp & 0xffff) < 4)
  53. printf("Warning: entering realmode with sp < 4 will fail\n");
  54. memcpy(REALMODE_MAILBOX, in, sizeof(struct pt_regs));
  55. asm("wbinvd\n");
  56. __asm__ volatile (
  57. "lcall $0x20,%0\n" : : "i" (&realmode_enter));
  58. asm("wbinvd\n");
  59. memcpy(out, REALMODE_MAILBOX, sizeof(struct pt_regs));
  60. return out->eax;
  61. }
  62. /*
  63. * This code is supposed to access a realmode interrupt
  64. * it does currently not work for me
  65. */
  66. int enter_realmode_int(u8 lvl, struct pt_regs *in, struct pt_regs *out)
  67. {
  68. /* place two instructions at 0x700 */
  69. writeb(0xcd, 0x700); /* int $lvl */
  70. writeb(lvl, 0x701);
  71. writeb(0xcb, 0x702); /* lret */
  72. asm("wbinvd\n");
  73. enter_realmode(0x00, 0x700, in, out);
  74. return out->eflags & 0x00000001;
  75. }