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

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

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

revision 1.6 by cshapiro, Mon Sep 5 06:09:13 2005 UTC revision 1.7 by rtoy, Thu Sep 15 18:26:51 2005 UTC
# Line 41  the particular section in. Line 41  the particular section in.
41  #include <sys/times.h>  #include <sys/times.h>
42  #include <errno.h>  #include <errno.h>
43    
44  os_vm_size_t os_vm_page_size=(-1);  os_vm_size_t os_vm_page_size = (-1);
45    
46  #define MAX_SEGMENTS 20  #define MAX_SEGMENTS 20
47  #define ALLOC_SIZE 0x10000  #define ALLOC_SIZE 0x10000
48  static struct segment {  static struct segment {
49    os_vm_address_t base;      os_vm_address_t base;
50    os_vm_size_t len;      os_vm_size_t len;
51    os_vm_address_t valid;      os_vm_address_t valid;
52    os_vm_address_t protected;      os_vm_address_t protected;
53  } segments[MAX_SEGMENTS];  } segments[MAX_SEGMENTS];
54    
55  void  void
56  os_init()  os_init()
57  {  {
58    int i;      int i;
59    os_vm_page_size=sysconf(_SC_PAGE_SIZE);  
60    for(i=0;i<MAX_SEGMENTS;i++)      os_vm_page_size = sysconf(_SC_PAGE_SIZE);
61      segments[i].len=0;      for (i = 0; i < MAX_SEGMENTS; i++)
62            segments[i].len = 0;
63  }  }
64    
65    
66  os_vm_address_t  os_vm_address_t os_validate(os_vm_address_t addr, os_vm_size_t len)
 os_validate(os_vm_address_t addr, os_vm_size_t len)  
67  {  {
68    int fd,i;      int fd, i;
69    caddr_t ret;      caddr_t ret;
70    
71    addr=os_trunc_to_page(addr);      addr = os_trunc_to_page(addr);
72    len=os_round_up_size_to_page(len);      len = os_round_up_size_to_page(len);
73    
74  #ifdef DEBUG  #ifdef DEBUG
75    printf("os_validate: addr: 0x%X, len: 0x%X, end: 0x%X\n",addr,len,addr+len);      printf("os_validate: addr: 0x%X, len: 0x%X, end: 0x%X\n", addr, len,
76  #endif             addr + len);
77    assert(addr!=NULL);  #endif
78    assert(len!=0);      assert(addr != NULL);
79        assert(len != 0);
80    for(i=0;i<MAX_SEGMENTS;i++)  
81      if(segments[i].len==0) break;      for (i = 0; i < MAX_SEGMENTS; i++)
82            if (segments[i].len == 0)
83    assert(i!=MAX_SEGMENTS);              break;
84    
85    segments[i].base=addr;      assert(i != MAX_SEGMENTS);
86    segments[i].len=len;  
87    segments[i].valid=addr;      segments[i].base = addr;
88    segments[i].protected=addr+len;      segments[i].len = len;
89    return addr;      segments[i].valid = addr;
90        segments[i].protected = addr + len;
91        return addr;
92  }  }
93    
94  void  void
95  os_invalidate(os_vm_address_t addr, os_vm_size_t len)  os_invalidate(os_vm_address_t addr, os_vm_size_t len)
96  {  {
97    assert(FALSE);      assert(FALSE);
98  }  }
99    
100  os_vm_address_t  os_vm_address_t
101  os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)  os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
102  {  {
103    int i;      int i;
104    
105    addr=os_trunc_to_page(addr);      addr = os_trunc_to_page(addr);
106    len=os_round_up_size_to_page(len);      len = os_round_up_size_to_page(len);
107    
108    if(mmap(addr,len,OS_VM_PROT_ALL,MAP_FILE|MAP_FIXED|MAP_PRIVATE,fd,      if (mmap(addr, len, OS_VM_PROT_ALL, MAP_FILE | MAP_FIXED | MAP_PRIVATE, fd,
109            (off_t)offset) == (os_vm_address_t)-1) {               (off_t) offset) == (os_vm_address_t) - 1) {
110      perror("mmap");          perror("mmap");
111      return NULL;          return NULL;
112    }      }
113    
114    for(i=0;i<MAX_SEGMENTS;i++)      for (i = 0; i < MAX_SEGMENTS; i++)
115      if(segments[i].len!=0 && segments[i].base==addr) break;          if (segments[i].len != 0 && segments[i].base == addr)
116                break;
117    
118    assert(i!=MAX_SEGMENTS);      assert(i != MAX_SEGMENTS);
119    assert(segments[i].valid==addr);      assert(segments[i].valid == addr);
120    
121    segments[i].valid=addr+len;      segments[i].valid = addr + len;
122  #ifdef DEBUG  #ifdef DEBUG
123    printf("os_map: addr: 0x%X, len: 0x%X, end: 0x%X\n",addr,len,addr+len);      printf("os_map: addr: 0x%X, len: 0x%X, end: 0x%X\n", addr, len, addr + len);
124  #endif  #endif
125    return addr;      return addr;
126  }  }
127    
128  void  void
129  os_flush_icache(os_vm_address_t address, os_vm_size_t length)  os_flush_icache(os_vm_address_t address, os_vm_size_t length)
130  {  {
131    sanctify_for_execution(address,length);      sanctify_for_execution(address, length);
132  }  }
133    
134  void  void
135  os_protect(os_vm_address_t addr, os_vm_size_t len, os_vm_prot_t prot)  os_protect(os_vm_address_t addr, os_vm_size_t len, os_vm_prot_t prot)
136  {  {
137    int i;      int i;
138    addr=os_trunc_to_page(addr);  
139    len=os_round_up_size_to_page(len);      addr = os_trunc_to_page(addr);
140        len = os_round_up_size_to_page(len);
141    for(i=0;i<MAX_SEGMENTS;i++)  
142      if(segments[i].base<=addr && addr<segments[i].base+segments[i].len)      for (i = 0; i < MAX_SEGMENTS; i++)
143        break;          if (segments[i].base <= addr
144                && addr < segments[i].base + segments[i].len) break;
145    assert(i!=MAX_SEGMENTS);  
146        assert(i != MAX_SEGMENTS);
147    if(prot) {  
148      assert(segments[i].base==addr && segments[i].len==len);      if (prot) {
149      segments[i].protected=addr+len;          assert(segments[i].base == addr && segments[i].len == len);
150    } else {          segments[i].protected = addr + len;
151      assert(segments[i].protected==addr+len);      } else {
152      segments[i].protected=addr;          assert(segments[i].protected == addr + len);
153    }          segments[i].protected = addr;
   
   if(addr<segments[i].valid)  
     if(mprotect(addr,segments[i].valid-addr,prot) == -1) {  
       perror("mprotect");  
       printf("segments[i].base: 0x%X\n",segments[i].base);  
       printf("segments[i].len: 0x%X\n",segments[i].len);  
       printf("segments[i].valid: 0x%X\n",segments[i].valid);  
       printf("addr: 0x%X, segments[i].valid-addr: 0x%X\n",addr,  
              segments[i].valid-addr);  
       printf("prot: 0x%X, len 0x%X\n",prot,len);  
       assert(FALSE);  
154      }      }
155    
156        if (addr < segments[i].valid)
157            if (mprotect(addr, segments[i].valid - addr, prot) == -1) {
158                perror("mprotect");
159                printf("segments[i].base: 0x%X\n", segments[i].base);
160                printf("segments[i].len: 0x%X\n", segments[i].len);
161                printf("segments[i].valid: 0x%X\n", segments[i].valid);
162                printf("addr: 0x%X, segments[i].valid-addr: 0x%X\n", addr,
163                       segments[i].valid - addr);
164                printf("prot: 0x%X, len 0x%X\n", prot, len);
165                assert(FALSE);
166            }
167  #ifdef DEBUG  #ifdef DEBUG
168    printf("os_protect: addr: 0x%X, len: 0x%X, end: 0x%X, prot 0x%X\n",      printf("os_protect: addr: 0x%X, len: 0x%X, end: 0x%X, prot 0x%X\n",
169           addr,len,addr+len,prot);             addr, len, addr + len, prot);
170  #endif  #endif
171  }  }
172    
173  boolean  boolean valid_addr(os_vm_address_t addr)
 valid_addr(os_vm_address_t addr)  
174  {  {
175    int i;      int i;
176    
177    for(i=0;i<MAX_SEGMENTS;i++)      for (i = 0; i < MAX_SEGMENTS; i++)
178      if(segments[i].base<=addr && addr<segments[i].base+segments[i].len)          if (segments[i].base <= addr
179        return TRUE;              && addr < segments[i].base + segments[i].len) return TRUE;
180    return FALSE;      return FALSE;
181  }  }
182    
183  void  void
184  segv_handler(int signal, int code, struct sigcontext *context)  segv_handler(int signal, int code, struct sigcontext *context)
185  {  {
186    int i;      int i;
187    os_vm_address_t addr,nvalid;      os_vm_address_t addr, nvalid;
188    sigsetmask(BLOCKABLE);  
189        sigsetmask(BLOCKABLE);
190    addr=(os_vm_address_t)context->sc_sl.sl_ss.ss_cr21;  /* verify this!!! */  
191    for(i=0;i<MAX_SEGMENTS;i++)      addr = (os_vm_address_t) context->sc_sl.sl_ss.ss_cr21;      /* verify this!!! */
192      if(segments[i].len!=0 && segments[i].base<=addr &&      for (i = 0; i < MAX_SEGMENTS; i++)
193         addr<segments[i].base+segments[i].len)          if (segments[i].len != 0 && segments[i].base <= addr &&
194        break;              addr < segments[i].base + segments[i].len)
195    if(i==MAX_SEGMENTS || addr<segments[i].valid)              break;
196      interrupt_handle_now(signal,code,context);      if (i == MAX_SEGMENTS || addr < segments[i].valid)
197    else if(segments[i].protected<=addr) {          interrupt_handle_now(signal, code, context);
198      if(!interrupt_maybe_gc(signal,code,context))      else if (segments[i].protected <= addr) {
199        interrupt_handle_now(signal,code,context);          if (!interrupt_maybe_gc(signal, code, context))
200    } else {              interrupt_handle_now(signal, code, context);
201      nvalid=((os_vm_address_t)(((long)(addr+ALLOC_SIZE))&~((long)(ALLOC_SIZE-1))));      } else {
202      if(nvalid>segments[i].protected) nvalid=segments[i].protected;          nvalid =
203                ((os_vm_address_t)
204                 (((long) (addr + ALLOC_SIZE)) & ~((long) (ALLOC_SIZE - 1))));
205            if (nvalid > segments[i].protected)
206                nvalid = segments[i].protected;
207    
208  #ifdef DEBUG  #ifdef DEBUG
209      printf("Mapping: addr: 0x%08x, old: 0x%08x, new: 0x%08x\n",          printf("Mapping: addr: 0x%08x, old: 0x%08x, new: 0x%08x\n",
210             addr,segments[i].valid,nvalid);                 addr, segments[i].valid, nvalid);
211  #endif  #endif
212    
213      if(mmap(segments[i].valid,nvalid-segments[i].valid,          if (mmap(segments[i].valid, nvalid - segments[i].valid,
214              OS_VM_PROT_ALL,MAP_ANONYMOUS|MAP_FIXED|MAP_PRIVATE,-1,0) ==                   OS_VM_PROT_ALL, MAP_ANONYMOUS | MAP_FIXED | MAP_PRIVATE, -1,
215         (os_vm_address_t)-1) {                   0) == (os_vm_address_t) - 1) {
216        perror("mmap");              perror("mmap");
217        printf("segments[i].base: 0x%X\n",segments[i].base);              printf("segments[i].base: 0x%X\n", segments[i].base);
218        printf("segments[i].len: 0x%X\n",segments[i].len);              printf("segments[i].len: 0x%X\n", segments[i].len);
219        printf("segments[i].valid: 0x%X\n",segments[i].valid);              printf("segments[i].valid: 0x%X\n", segments[i].valid);
220        printf("segments[i].protected: 0x%X\n",segments[i].protected);              printf("segments[i].protected: 0x%X\n", segments[i].protected);
221        printf("nvalid: 0x%X\n",nvalid);              printf("nvalid: 0x%X\n", nvalid);
222        printf("nvalid-segments[i].valid: 0x%X\n",nvalid-segments[i].valid);              printf("nvalid-segments[i].valid: 0x%X\n",
223        assert(FALSE);                     nvalid - segments[i].valid);
224                assert(FALSE);
225            }
226            segments[i].valid = nvalid;
227      }      }
     segments[i].valid=nvalid;  
   }  
228  }  }
229    
230  void  void
231  sigbus_handler(int signal, int code, struct sigcontext *context)  sigbus_handler(int signal, int code, struct sigcontext *context)
232  {  {
233  #ifdef DEBUG  #ifdef DEBUG
234    printf("Bus Error at 0x%X\n",context->sc_sl.sl_ss.ss_cr21);      printf("Bus Error at 0x%X\n", context->sc_sl.sl_ss.ss_cr21);
235  #endif  #endif
236    
237    if(!interrupt_maybe_gc(signal, code, context))      if (!interrupt_maybe_gc(signal, code, context))
238      interrupt_handle_now(signal, code, context);          interrupt_handle_now(signal, code, context);
239  }  }
240    
241  void  void
242  os_install_interrupt_handlers()  os_install_interrupt_handlers()
243  {  {
244    interrupt_install_low_level_handler(SIGSEGV,segv_handler);      interrupt_install_low_level_handler(SIGSEGV, segv_handler);
245    interrupt_install_low_level_handler(SIGBUS,sigbus_handler);      interrupt_install_low_level_handler(SIGBUS, sigbus_handler);
246  }  }
247    
248  void  void
249  os_zero(os_vm_address_t addr,os_vm_size_t length)  os_zero(os_vm_address_t addr, os_vm_size_t length)
250  {  {
251      os_vm_address_t block_start;      os_vm_address_t block_start;
252      os_vm_size_t block_size;      os_vm_size_t block_size;
253      int i;      int i;
254    
255  #ifdef PRINTNOISE  #ifdef PRINTNOISE
256      fprintf(stderr,"os_zero: addr: 0x%08x, len: 0x%08x, end: 0x%X\n",addr,      fprintf(stderr, "os_zero: addr: 0x%08x, len: 0x%08x, end: 0x%X\n", addr,
257              length,addr+length);              length, addr + length);
258  #endif  #endif
259    
260      block_start=os_round_up_to_page(addr);      block_start = os_round_up_to_page(addr);
261    
262      length-=block_start-addr;      length -= block_start - addr;
263      block_size=os_trunc_size_to_page(length);      block_size = os_trunc_size_to_page(length);
264    
265        if (block_start > addr)
266            memset((char *) addr, 0, block_start - addr);
267        if (block_size < length)
268            assert(FALSE);
269    
     if(block_start>addr)  
       memset((char *)addr,0,block_start-addr);  
     if(block_size<length)  
       assert(FALSE);  
   
270      if (block_size != 0) {      if (block_size != 0) {
271          /* Now deallocate and allocate the block so that it */          /* Now deallocate and allocate the block so that it */
272          /* faults in  zero-filled. */          /* faults in  zero-filled. */
273    
274          for(i=0;i<MAX_SEGMENTS;i++)          for (i = 0; i < MAX_SEGMENTS; i++)
275            if(segments[i].base<=block_start &&              if (segments[i].base <= block_start &&
276               block_start<segments[i].base+segments[i].len)                  block_start < segments[i].base + segments[i].len)
277              break;                  break;
278          assert(i!=MAX_SEGMENTS);          assert(i != MAX_SEGMENTS);
279          assert(block_start+block_size==segments[i].base+segments[i].len);          assert(block_start + block_size == segments[i].base + segments[i].len);
280    
281          if(segments[i].valid>block_start) {          if (segments[i].valid > block_start) {
282            if(munmap(block_start,segments[i].valid-block_start)==-1) {              if (munmap(block_start, segments[i].valid - block_start) == -1) {
283              perror("munmap");                  perror("munmap");
284              return;                  return;
285            }              }
286            segments[i].valid=block_start;              segments[i].valid = block_start;
287          }          }
288      }      }
289  }  }
290    
291  os_vm_address_t  os_vm_address_t os_allocate(os_vm_size_t len)
 os_allocate(os_vm_size_t len)  
292  {  {
293    return (os_vm_address_t) malloc(len);      return (os_vm_address_t) malloc(len);
294  }  }
295    
296  os_vm_address_t  os_vm_address_t os_allocate_at(os_vm_address_t addr, os_vm_size_t len)
 os_allocate_at(os_vm_address_t addr, os_vm_size_t len)  
297  {  {
298    return os_validate(addr, len);      return os_validate(addr, len);
299  }  }
300    
301  void  void
302  os_deallocate(os_vm_address_t addr,os_vm_size_t len)  os_deallocate(os_vm_address_t addr, os_vm_size_t len)
303  {  {
304    free(addr);      free(addr);
305  }  }
306    
307  os_vm_address_t  os_vm_address_t
308  os_reallocate(os_vm_address_t addr, os_vm_size_t old_len,  os_reallocate(os_vm_address_t addr, os_vm_size_t old_len, os_vm_size_t len)
309                                os_vm_size_t len)  {
310        addr = (os_vm_address_t) realloc(addr, len);
311        assert(addr != NULL);
312        return addr;
313    }
314    
315    int
316    getrusage(int who, struct rusage *rusage)
317  {  {
318    addr=(os_vm_address_t) realloc(addr,len);      static long ticks_per_sec = 0;
319    assert(addr!=NULL);      static long usec_per_tick = 0;
320    return addr;      struct tms buf;
321  }      clock_t uticks, sticks;
322    
323  int getrusage(int who,struct rusage *rusage)      memset(rusage, 0, sizeof(struct rusage));
324  {  
325    static long ticks_per_sec = 0;      if (ticks_per_sec == 0) {
326    static long usec_per_tick = 0;          ticks_per_sec = sysconf(_SC_CLK_TCK);
327    struct tms buf;          usec_per_tick = 1000000 / ticks_per_sec;
328    clock_t uticks, sticks;      }
329    
330    memset(rusage,0,sizeof(struct rusage));      if (times(&buf) == -1)
331    if (ticks_per_sec == 0) {          return -1;
332      ticks_per_sec = sysconf(_SC_CLK_TCK);  
333      usec_per_tick = 1000000 / ticks_per_sec;      if (who == RUSAGE_SELF) {
334    }          uticks = buf.tms_utime;
335            sticks = buf.tms_stime;
336    if(times(&buf) == -1)      } else if (who == RUSAGE_CHILDREN) {
337      return -1;          uticks = buf.tms_utime;
338            sticks = buf.tms_stime;
339    if(who == RUSAGE_SELF) {      } else {
340      uticks = buf.tms_utime;          errno = EINVAL;
341      sticks = buf.tms_stime;          return -1;
342    }      }
   else if(who == RUSAGE_CHILDREN) {  
     uticks = buf.tms_utime;  
     sticks = buf.tms_stime;  
   }  
   else {  
     errno = EINVAL;  
     return -1;  
   }  
   
   rusage->ru_utime.tv_sec = uticks / ticks_per_sec;  
   rusage->ru_utime.tv_usec = (uticks % ticks_per_sec) * usec_per_tick;  
   rusage->ru_stime.tv_sec = sticks / ticks_per_sec;  
   rusage->ru_stime.tv_usec = (sticks % ticks_per_sec) * usec_per_tick;  
343    
344    return 0;      rusage->ru_utime.tv_sec = uticks / ticks_per_sec;
345        rusage->ru_utime.tv_usec = (uticks % ticks_per_sec) * usec_per_tick;
346        rusage->ru_stime.tv_sec = sticks / ticks_per_sec;
347        rusage->ru_stime.tv_usec = (sticks % ticks_per_sec) * usec_per_tick;
348    
349        return 0;
350  }  }
351    
352  int  int
353  getdtablesize(void)  getdtablesize(void)
354  {  {
355    struct rlimit rlp;      struct rlimit rlp;
356    assert(getrlimit(RLIMIT_NOFILE,&rlp)==0);  
357    return rlp.rlim_cur;      assert(getrlimit(RLIMIT_NOFILE, &rlp) == 0);
358        return rlp.rlim_cur;
359  }  }
360    
361  unsigned long  unsigned long
362  gethostid(void)  gethostid(void)
363  {  {
364    char hostname[256];      char hostname[256];
365    struct hostent *hostent;      struct hostent *hostent;
366    static unsigned long addr=NULL;      static unsigned long addr = NULL;
367    
368    if(addr) return addr;      if (addr)
369            return addr;
370    if(gethostname(hostname, sizeof(hostname)) == -1) {  
371      perror("gethostname");      if (gethostname(hostname, sizeof(hostname)) == -1) {
372      return 0;          perror("gethostname");
373    }          return 0;
374        }
375    
376    hostent = gethostbyname(hostname);      hostent = gethostbyname(hostname);
377    if(hostent == NULL) {      if (hostent == NULL) {
378      perror("gethostbyname");          perror("gethostbyname");
379      return 0;          return 0;
380    }      }
381    
382    addr = ((unsigned char *)(hostent->h_addr))[0] << 24 |      addr = ((unsigned char *) (hostent->h_addr))[0] << 24 |
383           ((unsigned char *)(hostent->h_addr))[1] << 16 |          ((unsigned char *) (hostent->h_addr))[1] << 16 |
384           ((unsigned char *)(hostent->h_addr))[2] << 8 |          ((unsigned char *) (hostent->h_addr))[2] << 8 |
385           ((unsigned char *)(hostent->h_addr))[3];          ((unsigned char *) (hostent->h_addr))[3];
386    
387    return addr;      return addr;
388  }  }
389    
390  int  int
391  getpagesize(void)  getpagesize(void)
392  {  {
393    return os_vm_page_size;      return os_vm_page_size;
394  }  }

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

  ViewVC Help
Powered by ViewVC 1.1.5