home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / go32 / gormcb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-12  |  7.3 KB  |  197 lines

  1. /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
  2. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  3. #include <dpmi.h>
  4. #include <go32.h>
  5. #include <stdlib.h>
  6. #include <sys/exceptn.h>
  7.  
  8. /* This code really can't be nested since the RMCB structure isn't copied,
  9.    so the stack check isn't really useful.  But someone may fix it someday.
  10.    On entry CS is known to be ours, ES is probably ours (since we passed it),
  11.    SS:ESP is locked 4K stack.  ES:EDI is regs structure, DS:ESI is RM SS:SP.
  12.    Do NOT enable interrupts in the user routine.  Thanks to ctm@ardi.com for
  13.    the improvements.  C. Sandmann 3/95 */
  14.  
  15. #define    STACK_WAS_MALLOCED    (1 << 0)
  16.  
  17. #define    FILL    0x00
  18.  
  19. static unsigned char wrapper_common[] = {
  20. /* 00 */ 0x06,                /*     push    es               */
  21. /* 01 */ 0x1e,                /*     push    ds               */
  22. /* 02 */ 0x06,                /*     push    es               */
  23. /* 03 */ 0x1f,                /*     pop     ds               */
  24. /* 04 */ 0x66, 0xb8,            /*     mov ax,            */
  25. /* 06 */ FILL, FILL,            /*         _our_selector    */
  26. /* 08 */ 0x8e, 0xd8,            /*     mov ds, ax               */
  27. /* 0a */ 0xff, 0x05,             /*     incl            */
  28. /* 0c */ FILL, FILL, FILL, FILL,    /*        _call_count        */
  29. /* 10 */ 0x83, 0x3d,            /*     cmpl            */
  30. /* 12 */ FILL, FILL, FILL, FILL,    /*         _in_this_handler    */
  31. /* 16 */ 0x00,                /*         $0            */
  32. /* 17 */ 0x75,                /*     jne            */
  33. /* 18 */ 0x33,                /*         bypass        */
  34. /* 19 */ 0xc6, 0x05,            /*     movb            */
  35. /* 1b */ FILL, FILL, FILL, FILL,    /*         _in_this_handler     */
  36. /* 1f */ 0x01,                /*         $1            */
  37. /* 20 */ 0x8e, 0xc0,            /*     mov es, ax               */
  38. /* 22 */ 0x8e, 0xe0,            /*     mov fs, ax               */
  39. /* 24 */ 0x8e, 0xe8,            /*     mov gs, ax               */
  40. /* 26 */ 0xbb,                /*     mov ebx,            */
  41. /* 27 */ FILL, FILL, FILL, FILL,    /*         _local_stack        */
  42. /* 2b */ 0xfc,                /*     cld                      */
  43. /* 2c */ 0x89, 0xe1,            /*     mov ecx, esp             */
  44. /* 2e */ 0x8c, 0xd2,            /*     mov dx, ss               */
  45. /* 30 */ 0x8e, 0xd0,            /*     mov ss, ax               */
  46. /* 32 */ 0x89, 0xdc,            /*     mov esp, ebx             */
  47. /* 34 */ 0x52,                /*     push edx                 */
  48. /* 35 */ 0x51,                /*     push ecx                 */
  49. /* 36 */ 0x56,                /*     push esi                 */
  50. /* 37 */ 0x57,                /*     push edi                 */
  51. /* 38 */ 0xe8,                /*     call                */
  52. /* 39 */ FILL, FILL, FILL, FILL,    /*         _rmcb                */
  53. /* 3d */ 0x5f,                /*     pop edi                  */
  54. /* 3e */ 0x5e,                /*     pop esi                  */
  55. /* 3f */ 0x58,                /*     pop eax                     */
  56. /* 40 */ 0x5b,                /*     pop ebx                     */
  57. /* 41 */ 0x8e, 0xd3,            /*     mov ss, bx               */
  58. /* 43 */ 0x89, 0xc4,            /*     mov esp, eax             */
  59. /* 45 */ 0xc6, 0x05,            /*     movb            */
  60. /* 47 */ FILL, FILL, FILL, FILL,    /*         _in_this_handler    */
  61. /* 4b */ 0x00,                /*         $0            */
  62. /* 4c */ 0x1f,                    /* bypass:  pop ds              */
  63. /* 4d */ 0x07,                /*     pop es                   */
  64.  
  65. /* 4e */ 0x8b, 0x06,            /*     mov eax,[esi]            */
  66. /* 50 */ 0x26, 0x89, 0x47, 0x2a,    /*     mov es:[edi+42],eax      */
  67. };
  68.  
  69. static unsigned char wrapper_retf[] = {
  70.          0x66, 0x26, 0x83, 0x47, 0x2e, 0x04,    /* add     es:[edi+46],0x4 */
  71.          0xcf                        /* iret                    */
  72. };
  73.  
  74. static unsigned char wrapper_iret[] = {
  75.          0x66, 0x8b, 0x46, 0x04,        /* mov     ax,[esi+4]      */
  76.          0x66, 0x26, 0x89, 0x47, 0x20,        /* mov     es:[edi+32],ax  */
  77.          0x66, 0x26, 0x83, 0x47, 0x2e, 0x06,    /* add     es:[edi+46],0x6 */
  78.          0xcf                    /* iret                    */
  79. };
  80.  
  81. unsigned long _go32_rmcb_stack_size = 32256;
  82.  
  83. static int setup_rmcb(unsigned char *wrapper, _go32_dpmi_seginfo *info,
  84.   __dpmi_regs *regs, unsigned char *stack, unsigned long stack_length)
  85. {
  86. #define    MALLOC_STACK()                    \
  87.   do {                            \
  88.       if (!stack_length) {                \
  89.       stack_length = _go32_rmcb_stack_size;    \
  90.       stack = (char *)malloc(stack_length);        \
  91.       if (stack == 0) {                \
  92.         free(wrapper);                \
  93.         return 0x8015;                \
  94.       }                        \
  95.           if( _go32_dpmi_lock_data( stack,              \
  96.             stack_length) ) return 0x8015;              \
  97.       ((long *)stack)[0] = STACK_WAS_MALLOCED;    \
  98.       } else                        \
  99.       ((long *)stack)[0] = 0;            \
  100.       ((long *)stack)[1] = 0;                \
  101.       ((long *)stack)[2] = 0;                \
  102.   } while (0)
  103.  
  104.   MALLOC_STACK();
  105.   if( _go32_dpmi_lock_data(regs, sizeof(__dpmi_regs)))
  106.     return 0x8015;
  107.  
  108.   *(short *)(wrapper+0x06) = __djgpp_ds_alias;
  109.   *(long  *)(wrapper+0x0c) = (long) stack + 8;
  110.   *(long  *)(wrapper+0x12) = (long) stack + 4;
  111.   *(long  *)(wrapper+0x1b) = (long) stack + 4;
  112.   *(long  *)(wrapper+0x27) = (long) stack + stack_length;
  113.   *(long  *)(wrapper+0x39) = info->pm_offset - ((long)wrapper + 0x3d);
  114.   *(long  *)(wrapper+0x47) = (long) stack + 4;
  115.  
  116.   info->size = (int)wrapper;
  117.  
  118.   return __dpmi_allocate_real_mode_callback((void *)wrapper, regs,
  119.                                             (__dpmi_raddr *)&info->rm_offset);
  120. }
  121.  
  122.  
  123. static int _go32_dpmi_allocate_real_mode_callback_retf_with_stack(
  124.     _go32_dpmi_seginfo *info, __dpmi_regs *regs, unsigned char *stack,
  125.                             unsigned long stack_length)
  126. {
  127.   unsigned char *wrapper;
  128.  
  129. #define    CHECK_STACK()                            \
  130.   if ((stack_length && stack_length < 512) ||                \
  131.         (!stack_length && _go32_rmcb_stack_size < 512))    \
  132.     return 0x8015
  133.  
  134.   CHECK_STACK();
  135.  
  136.   wrapper = (unsigned char *)malloc(sizeof(wrapper_common) +
  137.                                  sizeof(wrapper_retf));
  138.   if (wrapper == 0)
  139.     return 0x8015;
  140.  
  141.   if( _go32_dpmi_lock_data( wrapper,
  142.     sizeof(wrapper_common) + sizeof(wrapper_retf)) ) return 0x8015;
  143.  
  144.   memcpy(wrapper, wrapper_common, sizeof(wrapper_common));
  145.   memcpy(wrapper+sizeof(wrapper_common), wrapper_retf, sizeof(wrapper_retf));
  146.  
  147.   return setup_rmcb(wrapper, info, regs, stack, stack_length);
  148. }
  149.  
  150. int _go32_dpmi_allocate_real_mode_callback_retf(_go32_dpmi_seginfo *info,
  151.                         __dpmi_regs *regs)
  152. {
  153.     return _go32_dpmi_allocate_real_mode_callback_retf_with_stack
  154.       (info, regs, (unsigned char *) 0, 0);
  155. }
  156.  
  157. static int _go32_dpmi_allocate_real_mode_callback_iret_with_stack(
  158.                               _go32_dpmi_seginfo *info, __dpmi_regs *regs,
  159.                               unsigned char *stack, unsigned long stack_length)
  160. {
  161.   unsigned char *wrapper;
  162.  
  163.   CHECK_STACK();
  164.  
  165.   wrapper = (unsigned char *)malloc(sizeof(wrapper_common) +
  166.                                  sizeof(wrapper_iret));
  167.   if (wrapper == 0)
  168.     return 0x8015;
  169.  
  170.   if( _go32_dpmi_lock_data( wrapper,
  171.     sizeof(wrapper_common) + sizeof(wrapper_iret)) ) return 0x8015;
  172.  
  173.   memcpy(wrapper, wrapper_common, sizeof(wrapper_common));
  174.   memcpy(wrapper+sizeof(wrapper_common), wrapper_iret, sizeof(wrapper_iret));
  175.  
  176.   return setup_rmcb(wrapper, info, regs, stack, stack_length);
  177. }
  178.  
  179. int _go32_dpmi_allocate_real_mode_callback_iret(_go32_dpmi_seginfo *info,
  180.                             __dpmi_regs *regs)
  181. {
  182.     return _go32_dpmi_allocate_real_mode_callback_iret_with_stack(info, regs,
  183.                                (unsigned char *) 0, 0);
  184. }
  185.  
  186. int _go32_dpmi_free_real_mode_callback(_go32_dpmi_seginfo *info)
  187. {
  188.   unsigned char *stack;
  189.  
  190.   stack = (char *)(*(long *)((long) info->size+0x12) - 4);
  191.   if (*(long *) stack & STACK_WAS_MALLOCED)
  192.       free(stack);
  193.  
  194.   free((char *)info->size);
  195.   return __dpmi_free_real_mode_callback((__dpmi_raddr *)&info->rm_offset);
  196. }
  197.