97d6a0519ce23e664a3406c1a9f7f7b84ef3bccc
[projects/cmucl/cmucl.git] / src / lisp / irix-os.c
1 /*
2  * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/irix-os.c,v 1.7 2011/09/01 05:18:26 rtoy Exp $
3  *
4  * OS-dependent routines.  This file (along with os.h) exports an
5  * OS-independent interface to the operating system VM facilities.
6  * Suprisingly, this interface looks a lot like the Mach interface
7  * (but simpler in some places).  For some operating systems, a subset
8  * of these functions will have to be emulated.
9  *
10  * This is the IRIX version.  By Sean Hallgren.
11  *
12  */
13
14 #include <stdio.h>
15 #include <sys/file.h>
16 #include <errno.h>
17 #include <signal.h>
18 #include "os.h"
19 #include "arch.h"
20 #include "interrupt.h"
21 #include "lispregs.h"
22 #include <sys/types.h>
23 #include <sys/sysinfo.h>
24 #include <sys/proc.h>
25
26 /* #define DEBUG */
27
28 os_vm_size_t os_vm_page_size = (-1);
29
30 int zero_fd;
31
32 void
33 os_init0(const char *argv[], const char *envp[])
34 {}
35
36 void
37 os_init(const char *argv[], const char *envp[])
38 {
39     zero_fd = open("/dev/zero", O_RDONLY);
40     os_vm_page_size = getpagesize();
41 }
42
43 os_vm_address_t os_validate(os_vm_address_t addr, os_vm_size_t len)
44 {
45     int flags = MAP_PRIVATE | MAP_AUTORESRV;
46
47 #ifdef DEBUG
48     printf("os_validate: addr = %x, len = %x\n", addr, len);
49 #endif
50
51     if (addr)
52         flags |= MAP_FIXED;
53
54     if ((addr = mmap(addr, len, OS_VM_PROT_ALL, flags, zero_fd, 0)) ==
55         (os_vm_address_t) - 1)
56         perror("mmap");
57
58     return addr;
59 }
60
61 void
62 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
63 {
64 #ifdef DEBUG
65     printf("os_invalidate: addr = %x, len = %x\n", addr, len);
66 #endif
67
68     if (munmap(addr, len) == -1)
69         perror("munmap");
70 }
71
72 os_vm_address_t
73 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
74 {
75 #ifdef DEBUG
76     printf("os_map: fd = %d, offset = %d, addr = %x, len = %x\n", fd, offset,
77            addr, len);
78 #endif
79
80     if ((addr = mmap(addr, len, OS_VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, fd,
81                      (off_t) offset)) == (os_vm_address_t) - 1)
82         perror("mmap");
83
84     return addr;
85 }
86
87 void
88 sanctify_for_execution(os_vm_address_t addr, os_vm_size_t len)
89 {
90     char *end_addr = addr + len;
91
92     addr = os_trunc_to_page(addr);
93     len = end_addr - addr;
94
95     if (mprotect(addr, len, OS_VM_PROT_ALL) == -1)
96         perror("mprotect");
97 }
98
99 void
100 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
101 {
102     sanctify_for_execution(address, length);
103 }
104
105 void
106 os_protect(os_vm_address_t addr, os_vm_size_t len, os_vm_prot_t prot)
107 {
108     if (mprotect(addr, len, prot) == -1)
109         perror("mprotect");
110 }
111
112 boolean valid_addr(os_vm_address_t addr)
113 {
114 }
115
116 static void
117 sigbus_handler(int signal, int code, struct sigcontext *context)
118 {
119     if (!interrupt_maybe_gc(signal, code, context))
120         interrupt_handle_now(signal, code, context);
121 }
122
123 static void
124 sigsegv_handler(int signal, int code, struct sigcontext *context)
125 {
126     if (!interrupt_maybe_gc(signal, code, context))
127         interrupt_handle_now(signal, code, context);
128 }
129
130 void
131 os_install_interrupt_handlers(void)
132 {
133     interrupt_install_low_level_handler(SIGSEGV, sigsegv_handler);
134     interrupt_install_low_level_handler(SIGBUS, sigbus_handler);
135 }