/[cmucl]/src/lisp/FreeBSD-os.c
ViewVC logotype

Diff of /src/lisp/FreeBSD-os.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.9 by moore, Wed Aug 28 07:16:38 2002 UTC revision 1.10 by gerd, Sun Mar 23 21:23:41 2003 UTC
# Line 34  Line 34 
34  #include <sys/proc.h>  #include <sys/proc.h>
35  #include <dlfcn.h>  #include <dlfcn.h>
36  #include "validate.h"  #include "validate.h"
 vm_size_t os_vm_page_size;  
   
 #define DPRINTF(t,a) {if (t) fprintf a;}  
37    
38  #if defined GENCGC  #if defined GENCGC
39  #include "gencgc.h"  #include "gencgc.h"
# Line 48  vm_size_t os_vm_page_size; Line 45  vm_size_t os_vm_page_size;
45  #undef errno  #undef errno
46  int errno;  int errno;
47  #endif  #endif
48    
49    vm_size_t os_vm_page_size;
50    
51    
52  void os_init(void)  void
53    os_init (void)
54  {  {
55    os_vm_page_size = OS_VM_DEFAULT_PAGESIZE;    os_vm_page_size = getpagesize ();
56  }  }
57    
58  int sc_reg(struct sigcontext *c, int offset)  int
59    sc_reg (struct sigcontext *c, int offset)
60  {  {
61    switch(offset)    switch(offset)
62      {      {
# Line 67  int sc_reg(struct sigcontext *c, int off Line 69  int sc_reg(struct sigcontext *c, int off
69      case 12: return c->sc_esi;      case 12: return c->sc_esi;
70      case 14: return c->sc_edi;      case 14: return c->sc_edi;
71      }      }
72    
73    return 0;    return 0;
74  }  }
75    
76  void os_save_context(void)  void
77    os_save_context (void)
78  {  {
79    /*    /* Called from interrupt handlers so C stuff knows things set in
80     * Called from interrupt handlers so C stuff knows things set in Lisp.       Lisp.  */
    */  
81  }  }
82    
83  void os_set_context(void)  void
84    os_set_context (void)
85  {  {
86  }  }
87    
88  os_vm_address_t os_validate(os_vm_address_t addr, os_vm_size_t len)  os_vm_address_t
89    os_validate (os_vm_address_t addr, os_vm_size_t len)
90  {  {
91    int flags = MAP_PRIVATE | MAP_ANONYMOUS;    int flags = MAP_PRIVATE | MAP_ANONYMOUS;
92    
# Line 90  os_vm_address_t os_validate(os_vm_addres Line 95  os_vm_address_t os_validate(os_vm_addres
95    else    else
96      flags |= MAP_VARIABLE;      flags |= MAP_VARIABLE;
97    
98    DPRINTF(0, (stderr, "os_validate %x %d => ", addr, len));    addr = mmap (addr, len, OS_VM_PROT_ALL, flags, -1, 0);
   
   addr = mmap(addr, len, OS_VM_PROT_ALL, flags, -1, 0);  
99    
100    if (addr == (os_vm_address_t) -1)    if (addr == (os_vm_address_t) -1)
101      {      {
102        perror("mmap");        perror ("mmap");
103        return NULL;        return NULL;
104      }      }
105    
   DPRINTF(0, (stderr, "%x\n", addr));  
   
106    return addr;    return addr;
107  }  }
108    
109  void os_invalidate(os_vm_address_t addr, os_vm_size_t len)  void
110    os_invalidate (os_vm_address_t addr, os_vm_size_t len)
111  {  {
112    DPRINTF(0, (stderr, "os_invalidate %x %d\n", addr, len));    if (munmap (addr, len) == -1)
113        perror ("munmap");
   if (munmap(addr, len) == -1)  
     perror("munmap");  
114  }  }
115    
116  os_vm_address_t os_map(int fd, int offset, os_vm_address_t addr,  os_vm_address_t
117                         os_vm_size_t len)  os_map (int fd, int offset, os_vm_address_t addr,
118            os_vm_size_t len)
119  {  {
120    addr = mmap(addr, len,    addr = mmap (addr, len, OS_VM_PROT_ALL,
121                OS_VM_PROT_ALL,                 MAP_PRIVATE | MAP_FILE | MAP_FIXED,
122                MAP_PRIVATE | MAP_FILE | MAP_FIXED,                 fd, (off_t) offset);
               fd, (off_t) offset);  
123    
124    if (addr == (os_vm_address_t) -1)    if (addr == (os_vm_address_t) -1)
125      perror("mmap");      perror ("mmap");
126    
127    return addr;    return addr;
128  }  }
129    
130  void os_flush_icache(os_vm_address_t address, os_vm_size_t length)  void
131    os_flush_icache (os_vm_address_t address, os_vm_size_t length)
132  {  {
133  }  }
134    
135  void os_protect(os_vm_address_t address, os_vm_size_t length,  void
136                  os_vm_prot_t prot)  os_protect (os_vm_address_t address, os_vm_size_t length,
137                os_vm_prot_t prot)
138  {  {
139    if (mprotect(address, length, prot) == -1)    if (mprotect (address, length, prot) == -1)
140      perror("mprotect");      perror ("mprotect");
141  }  }
142    
143    
144    
145  static boolean in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)  static boolean
146    in_range_p (os_vm_address_t a, lispobj sbeg, size_t slen)
147  {  {
148    char* beg = (char*) sbeg;    char *beg = (char *) sbeg;
149    char* end = (char*) sbeg + slen;    char *end = (char *) sbeg + slen;
150    char* adr = (char*) a;    char *adr = (char *) a;
151    return (adr >= beg && adr < end);    return adr >= beg && adr < end;
152  }  }
153    
154  boolean valid_addr(os_vm_address_t addr)  boolean
155    valid_addr (os_vm_address_t addr)
156  {  {
157    int ret;    int ret;
158    os_vm_address_t newaddr;    os_vm_address_t newaddr;
159    newaddr = os_trunc_to_page(addr);    newaddr = os_trunc_to_page (addr);
160    
161    if (   in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE)    if (in_range_p (addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE)
162        || in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE   )        || in_range_p (addr, STATIC_SPACE_START , STATIC_SPACE_SIZE)
163        || in_range_p(addr, DYNAMIC_0_SPACE_START, dynamic_space_size  )        || in_range_p (addr, DYNAMIC_0_SPACE_START, dynamic_space_size)
164        || in_range_p(addr, DYNAMIC_1_SPACE_START, dynamic_space_size  )  #ifndef GENCGC
165        || in_range_p(addr, CONTROL_STACK_START  , CONTROL_STACK_SIZE  )        || in_range_p (addr, DYNAMIC_1_SPACE_START, dynamic_space_size)
166        || in_range_p(addr, BINDING_STACK_START  , BINDING_STACK_SIZE  ))  #endif
167          || in_range_p (addr, CONTROL_STACK_START, CONTROL_STACK_SIZE)
168          || in_range_p (addr, BINDING_STACK_START, BINDING_STACK_SIZE))
169      return TRUE;      return TRUE;
170    return FALSE;    return FALSE;
171  }  }
172    
173    
174  static void sigbus_handler(int signal, int code, struct sigcontext *context,  static void
175                             void *fault_addr)  sigbus_handler (int signal, int code, struct sigcontext *context,
176  {                  void *fault_addr)
177  #if defined GENCGC  {
178    int  page_index = find_page_index(fault_addr);    int page_index;
179    
180  #if SIGBUS_VERBOSE  #ifdef RED_ZONE_HIT
181    fprintf(stderr,"Signal %d, fault_addr=%x, page_index=%d:\n",    if (os_control_stack_overflow (fault_addr, context))
182            signal, fault_addr, page_index);      return;
183  #endif  #endif
184    
185    #if defined GENCGC
186      page_index = find_page_index(fault_addr);
187    
188    /* Check if the fault is within the dynamic space. */    /* Check if the fault is within the dynamic space. */
189    if (page_index != -1) {    if (page_index != -1)
190      /* Un-protect the page */      {
191          /* Un-protect the page */
192    
193      /* The page should have been marked write protected */        /* The page should have been marked write protected */
194      if (!PAGE_WRITE_PROTECTED(page_index))        if (!PAGE_WRITE_PROTECTED (page_index))
195        fprintf(stderr, "*** Sigbus in page not marked as write protected\n");          fprintf (stderr, "*** Sigbus in page not marked as write protected\n");
   
     os_protect(page_address(page_index), 4096, OS_VM_PROT_ALL);  
     page_table[page_index].flags &= ~PAGE_WRITE_PROTECTED_MASK;  
     page_table[page_index].flags |= PAGE_WRITE_PROTECT_CLEARED_MASK;  
196    
197      return;        os_protect (page_address (page_index), 4096, OS_VM_PROT_ALL);
198          page_table[page_index].flags &= ~PAGE_WRITE_PROTECTED_MASK;
199          page_table[page_index].flags |= PAGE_WRITE_PROTECT_CLEARED_MASK;
200    
201          return;
202      }      }
203  #endif  #endif /* GENCGC */
204    
205    DPRINTF(0, (stderr, "sigbus:\n"));    interrupt_handle_now (signal, code, context);
   interrupt_handle_now(signal, code, context);  
206  }  }
207    
208  static void sigsegv_handler(int signal, int code, struct sigcontext *context)  static void
209    sigsegv_handler (int signal, int code, struct sigcontext *context)
210  {  {
211    DPRINTF(0, (stderr, "os_sigsegv\n"));    interrupt_handle_now (signal, code, context);
   interrupt_handle_now(signal, code, context);  
212  }  }
213    
214  void os_install_interrupt_handlers(void)  void
215    os_install_interrupt_handlers (void)
216  {  {
217    interrupt_install_low_level_handler(SIGSEGV, sigsegv_handler);    interrupt_install_low_level_handler
218    interrupt_install_low_level_handler(SIGBUS, sigbus_handler);      (SIGSEGV, (void (*) (HANDLER_ARGS)) sigsegv_handler);
219      interrupt_install_low_level_handler
220        (SIGBUS, (void (*) (HANDLER_ARGS)) sigbus_handler);
221  }  }
222    
223  void *os_dlsym(const char *sym_name, lispobj lib_list)  void *
224    os_dlsym (const char *sym_name, lispobj lib_list)
225  {  {
226      if (lib_list != NIL) {    if (lib_list != NIL)
227          lispobj lib_list_head;      {
228          lispobj lib_list_head;
229    
230          for (lib_list_head = lib_list;        for (lib_list_head = lib_list;
231               lib_list_head != NIL;             lib_list_head != NIL;
232               lib_list_head = (CONS(lib_list_head))->cdr) {             lib_list_head = CONS (lib_list_head)->cdr)
233              struct cons *lib_cons = CONS(CONS(lib_list_head)->car);          {
234              struct sap *dlhandle = (struct sap *)PTR(lib_cons->car);            struct cons *lib_cons = CONS (CONS (lib_list_head)->car);
235              void *sym_addr = dlsym((void *)dlhandle->pointer, sym_name);            struct sap *dlhandle = (struct sap *) PTR (lib_cons->car);
236              void *sym_addr = dlsym ((void *) dlhandle->pointer, sym_name);
237    
238              if (sym_addr)            if (sym_addr)
239                  return sym_addr;              return sym_addr;
240          }          }
241      }      }
242      return dlsym(RTLD_DEFAULT, sym_name);  
243      return dlsym (RTLD_DEFAULT, sym_name);
244  }  }

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

  ViewVC Help
Powered by ViewVC 1.1.5