You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

com32_call.c 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (C) 2008 Daniel Verkamp <daniel@drv.nu>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /**
  19. * @file SYSLINUX COM32 helpers
  20. *
  21. */
  22. FILE_LICENCE ( GPL2_OR_LATER );
  23. #include <stdint.h>
  24. #include <realmode.h>
  25. #include <comboot.h>
  26. #include <assert.h>
  27. #include <ipxe/uaccess.h>
  28. static com32sys_t __bss16 ( com32_regs );
  29. #define com32_regs __use_data16 ( com32_regs )
  30. static uint8_t __bss16 ( com32_int_vector );
  31. #define com32_int_vector __use_data16 ( com32_int_vector )
  32. static uint32_t __bss16 ( com32_farcall_proc );
  33. #define com32_farcall_proc __use_data16 ( com32_farcall_proc )
  34. uint16_t __bss16 ( com32_saved_sp );
  35. /**
  36. * Interrupt call helper
  37. */
  38. void __asmcall com32_intcall ( uint8_t interrupt, physaddr_t inregs_phys, physaddr_t outregs_phys ) {
  39. memcpy_user ( virt_to_user( &com32_regs ), 0,
  40. phys_to_user ( inregs_phys ), 0,
  41. sizeof(com32sys_t) );
  42. com32_int_vector = interrupt;
  43. __asm__ __volatile__ (
  44. REAL_CODE ( /* Save all registers */
  45. "pushal\n\t"
  46. "pushw %%ds\n\t"
  47. "pushw %%es\n\t"
  48. "pushw %%fs\n\t"
  49. "pushw %%gs\n\t"
  50. /* Mask off unsafe flags */
  51. "movl (com32_regs + 40), %%eax\n\t"
  52. "andl $0x200cd7, %%eax\n\t"
  53. "movl %%eax, (com32_regs + 40)\n\t"
  54. /* Load com32_regs into the actual registers */
  55. "movw %%sp, %%ss:(com32_saved_sp)\n\t"
  56. "movw $com32_regs, %%sp\n\t"
  57. "popw %%gs\n\t"
  58. "popw %%fs\n\t"
  59. "popw %%es\n\t"
  60. "popw %%ds\n\t"
  61. "popal\n\t"
  62. "popfl\n\t"
  63. "movw %%ss:(com32_saved_sp), %%sp\n\t"
  64. /* patch INT instruction */
  65. "pushw %%ax\n\t"
  66. "movb %%ss:(com32_int_vector), %%al\n\t"
  67. "movb %%al, %%cs:(com32_intcall_instr + 1)\n\t"
  68. /* perform a jump to avoid problems with cache
  69. * consistency in self-modifying code on some CPUs (486)
  70. */
  71. "jmp 1f\n"
  72. "1:\n\t"
  73. "popw %%ax\n\t"
  74. "com32_intcall_instr:\n\t"
  75. /* INT instruction to be patched */
  76. "int $0xFF\n\t"
  77. /* Copy regs back to com32_regs */
  78. "movw %%sp, %%ss:(com32_saved_sp)\n\t"
  79. "movw $(com32_regs + 44), %%sp\n\t"
  80. "pushfl\n\t"
  81. "pushal\n\t"
  82. "pushw %%ds\n\t"
  83. "pushw %%es\n\t"
  84. "pushw %%fs\n\t"
  85. "pushw %%gs\n\t"
  86. "movw %%ss:(com32_saved_sp), %%sp\n\t"
  87. /* Restore registers */
  88. "popw %%gs\n\t"
  89. "popw %%fs\n\t"
  90. "popw %%es\n\t"
  91. "popw %%ds\n\t"
  92. "popal\n\t")
  93. : : );
  94. if ( outregs_phys ) {
  95. memcpy_user ( phys_to_user ( outregs_phys ), 0,
  96. virt_to_user( &com32_regs ), 0,
  97. sizeof(com32sys_t) );
  98. }
  99. }
  100. /**
  101. * Farcall helper
  102. */
  103. void __asmcall com32_farcall ( uint32_t proc, physaddr_t inregs_phys, physaddr_t outregs_phys ) {
  104. memcpy_user ( virt_to_user( &com32_regs ), 0,
  105. phys_to_user ( inregs_phys ), 0,
  106. sizeof(com32sys_t) );
  107. com32_farcall_proc = proc;
  108. __asm__ __volatile__ (
  109. REAL_CODE ( /* Save all registers */
  110. "pushal\n\t"
  111. "pushw %%ds\n\t"
  112. "pushw %%es\n\t"
  113. "pushw %%fs\n\t"
  114. "pushw %%gs\n\t"
  115. /* Mask off unsafe flags */
  116. "movl (com32_regs + 40), %%eax\n\t"
  117. "andl $0x200cd7, %%eax\n\t"
  118. "movl %%eax, (com32_regs + 40)\n\t"
  119. /* Load com32_regs into the actual registers */
  120. "movw %%sp, %%ss:(com32_saved_sp)\n\t"
  121. "movw $com32_regs, %%sp\n\t"
  122. "popw %%gs\n\t"
  123. "popw %%fs\n\t"
  124. "popw %%es\n\t"
  125. "popw %%ds\n\t"
  126. "popal\n\t"
  127. "popfl\n\t"
  128. "movw %%ss:(com32_saved_sp), %%sp\n\t"
  129. /* Call procedure */
  130. "lcall *%%ss:(com32_farcall_proc)\n\t"
  131. /* Copy regs back to com32_regs */
  132. "movw %%sp, %%ss:(com32_saved_sp)\n\t"
  133. "movw $(com32_regs + 44), %%sp\n\t"
  134. "pushfl\n\t"
  135. "pushal\n\t"
  136. "pushw %%ds\n\t"
  137. "pushw %%es\n\t"
  138. "pushw %%fs\n\t"
  139. "pushw %%gs\n\t"
  140. "movw %%ss:(com32_saved_sp), %%sp\n\t"
  141. /* Restore registers */
  142. "popw %%gs\n\t"
  143. "popw %%fs\n\t"
  144. "popw %%es\n\t"
  145. "popw %%ds\n\t"
  146. "popal\n\t")
  147. : : );
  148. if ( outregs_phys ) {
  149. memcpy_user ( phys_to_user ( outregs_phys ), 0,
  150. virt_to_user( &com32_regs ), 0,
  151. sizeof(com32sys_t) );
  152. }
  153. }
  154. /**
  155. * CDECL farcall helper
  156. */
  157. int __asmcall com32_cfarcall ( uint32_t proc, physaddr_t stack, size_t stacksz ) {
  158. int32_t eax;
  159. copy_user_to_rm_stack ( phys_to_user ( stack ), stacksz );
  160. com32_farcall_proc = proc;
  161. __asm__ __volatile__ (
  162. REAL_CODE ( "lcall *%%ss:(com32_farcall_proc)\n\t" )
  163. : "=a" (eax)
  164. :
  165. : "ecx", "edx" );
  166. remove_user_from_rm_stack ( 0, stacksz );
  167. return eax;
  168. }