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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. /**
  20. * @file SYSLINUX COM32 helpers
  21. *
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER );
  24. #include <stdint.h>
  25. #include <realmode.h>
  26. #include <comboot.h>
  27. #include <assert.h>
  28. #include <ipxe/uaccess.h>
  29. static com32sys_t __bss16 ( com32_regs );
  30. #define com32_regs __use_data16 ( com32_regs )
  31. static uint8_t __bss16 ( com32_int_vector );
  32. #define com32_int_vector __use_data16 ( com32_int_vector )
  33. static uint32_t __bss16 ( com32_farcall_proc );
  34. #define com32_farcall_proc __use_data16 ( com32_farcall_proc )
  35. uint16_t __bss16 ( com32_saved_sp );
  36. /**
  37. * Interrupt call helper
  38. */
  39. void __asmcall com32_intcall ( uint8_t interrupt, physaddr_t inregs_phys, physaddr_t outregs_phys ) {
  40. memcpy_user ( virt_to_user( &com32_regs ), 0,
  41. phys_to_user ( inregs_phys ), 0,
  42. sizeof(com32sys_t) );
  43. com32_int_vector = interrupt;
  44. __asm__ __volatile__ (
  45. REAL_CODE ( /* Save all registers */
  46. "pushal\n\t"
  47. "pushw %%ds\n\t"
  48. "pushw %%es\n\t"
  49. "pushw %%fs\n\t"
  50. "pushw %%gs\n\t"
  51. /* Mask off unsafe flags */
  52. "movl (com32_regs + 40), %%eax\n\t"
  53. "andl $0x200cd7, %%eax\n\t"
  54. "movl %%eax, (com32_regs + 40)\n\t"
  55. /* Load com32_regs into the actual registers */
  56. "movw %%sp, %%ss:(com32_saved_sp)\n\t"
  57. "movw $com32_regs, %%sp\n\t"
  58. "popw %%gs\n\t"
  59. "popw %%fs\n\t"
  60. "popw %%es\n\t"
  61. "popw %%ds\n\t"
  62. "popal\n\t"
  63. "popfl\n\t"
  64. "movw %%ss:(com32_saved_sp), %%sp\n\t"
  65. /* patch INT instruction */
  66. "pushw %%ax\n\t"
  67. "movb %%ss:(com32_int_vector), %%al\n\t"
  68. "movb %%al, %%cs:(com32_intcall_instr + 1)\n\t"
  69. /* perform a jump to avoid problems with cache
  70. * consistency in self-modifying code on some CPUs (486)
  71. */
  72. "jmp 1f\n"
  73. "1:\n\t"
  74. "popw %%ax\n\t"
  75. "com32_intcall_instr:\n\t"
  76. /* INT instruction to be patched */
  77. "int $0xFF\n\t"
  78. /* Copy regs back to com32_regs */
  79. "movw %%sp, %%ss:(com32_saved_sp)\n\t"
  80. "movw $(com32_regs + 44), %%sp\n\t"
  81. "pushfl\n\t"
  82. "pushal\n\t"
  83. "pushw %%ds\n\t"
  84. "pushw %%es\n\t"
  85. "pushw %%fs\n\t"
  86. "pushw %%gs\n\t"
  87. "movw %%ss:(com32_saved_sp), %%sp\n\t"
  88. /* Restore registers */
  89. "popw %%gs\n\t"
  90. "popw %%fs\n\t"
  91. "popw %%es\n\t"
  92. "popw %%ds\n\t"
  93. "popal\n\t")
  94. : : );
  95. if ( outregs_phys ) {
  96. memcpy_user ( phys_to_user ( outregs_phys ), 0,
  97. virt_to_user( &com32_regs ), 0,
  98. sizeof(com32sys_t) );
  99. }
  100. }
  101. /**
  102. * Farcall helper
  103. */
  104. void __asmcall com32_farcall ( uint32_t proc, physaddr_t inregs_phys, physaddr_t outregs_phys ) {
  105. memcpy_user ( virt_to_user( &com32_regs ), 0,
  106. phys_to_user ( inregs_phys ), 0,
  107. sizeof(com32sys_t) );
  108. com32_farcall_proc = proc;
  109. __asm__ __volatile__ (
  110. REAL_CODE ( /* Save all registers */
  111. "pushal\n\t"
  112. "pushw %%ds\n\t"
  113. "pushw %%es\n\t"
  114. "pushw %%fs\n\t"
  115. "pushw %%gs\n\t"
  116. /* Mask off unsafe flags */
  117. "movl (com32_regs + 40), %%eax\n\t"
  118. "andl $0x200cd7, %%eax\n\t"
  119. "movl %%eax, (com32_regs + 40)\n\t"
  120. /* Load com32_regs into the actual registers */
  121. "movw %%sp, %%ss:(com32_saved_sp)\n\t"
  122. "movw $com32_regs, %%sp\n\t"
  123. "popw %%gs\n\t"
  124. "popw %%fs\n\t"
  125. "popw %%es\n\t"
  126. "popw %%ds\n\t"
  127. "popal\n\t"
  128. "popfl\n\t"
  129. "movw %%ss:(com32_saved_sp), %%sp\n\t"
  130. /* Call procedure */
  131. "lcall *%%ss:(com32_farcall_proc)\n\t"
  132. /* Copy regs back to com32_regs */
  133. "movw %%sp, %%ss:(com32_saved_sp)\n\t"
  134. "movw $(com32_regs + 44), %%sp\n\t"
  135. "pushfl\n\t"
  136. "pushal\n\t"
  137. "pushw %%ds\n\t"
  138. "pushw %%es\n\t"
  139. "pushw %%fs\n\t"
  140. "pushw %%gs\n\t"
  141. "movw %%ss:(com32_saved_sp), %%sp\n\t"
  142. /* Restore registers */
  143. "popw %%gs\n\t"
  144. "popw %%fs\n\t"
  145. "popw %%es\n\t"
  146. "popw %%ds\n\t"
  147. "popal\n\t")
  148. : : );
  149. if ( outregs_phys ) {
  150. memcpy_user ( phys_to_user ( outregs_phys ), 0,
  151. virt_to_user( &com32_regs ), 0,
  152. sizeof(com32sys_t) );
  153. }
  154. }
  155. /**
  156. * CDECL farcall helper
  157. */
  158. int __asmcall com32_cfarcall ( uint32_t proc, physaddr_t stack, size_t stacksz ) {
  159. int32_t eax;
  160. copy_user_to_rm_stack ( phys_to_user ( stack ), stacksz );
  161. com32_farcall_proc = proc;
  162. __asm__ __volatile__ (
  163. REAL_CODE ( "lcall *%%ss:(com32_farcall_proc)\n\t" )
  164. : "=a" (eax)
  165. :
  166. : "ecx", "edx" );
  167. remove_user_from_rm_stack ( 0, stacksz );
  168. return eax;
  169. }
  170. /**
  171. * IRQ handler
  172. */
  173. void __asmcall com32_irq ( uint32_t vector ) {
  174. uint32_t *ivt_entry = phys_to_virt( vector * 4 );
  175. __asm__ __volatile__ (
  176. REAL_CODE ( "pushfw\n\t"
  177. "pushw %%cs\n\t"
  178. "pushw $com32_irq_return\n\t"
  179. "pushl %0\n\t"
  180. "lret\n"
  181. "com32_irq_return:\n\t" )
  182. : /* no outputs */
  183. : "r" ( *ivt_entry ) );
  184. }