realmode.c 2.5 KB

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