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

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