2 * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/mach-os.c,v 1.9 2011/09/01 05:18:26 rtoy Exp $
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.
10 * This is the Mach version.
20 #include "interrupt.h"
24 static struct segment {
28 static int segments = -1;
30 vm_size_t os_vm_page_size;
32 #if defined(i386) || defined(parisc)
36 return mach_task_self();
41 os_init0(const char *argv[], const char *envp[])
45 os_init(const char *argv[], const char *envp[])
47 os_vm_page_size = vm_page_size;
51 os_validate(vm_address_t addr, vm_size_t len)
55 res = vm_allocate(task_self(), &addr, len, addr == NULL);
57 if (res != KERN_SUCCESS)
62 vm_protect(task_self(), addr, len, FALSE,
63 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
69 os_invalidate(vm_address_t addr, vm_size_t len)
73 res = vm_deallocate(task_self(), addr, len);
75 if (res != KERN_SUCCESS)
76 mach_error("Could not vm_allocate memory: ", res);
82 os_map(int fd, int offset, vm_address_t addr, vm_size_t len)
86 res = map_fd(fd, offset, &addr, 0, len);
88 if (res != KERN_SUCCESS) {
91 sprintf(buf, "Could not map_fd(%d, %d, 0x%08x, 0x%08x): ",
92 fd, offset, addr, len);
95 lseek(fd, offset, L_SET);
101 vm_protect(task_self(), addr, len, FALSE,
102 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
108 os_flush_icache(vm_address_t address, vm_size_t length)
111 vm_machine_attribute_val_t flush;
114 flush = MATTR_VAL_ICACHE_FLUSH;
116 kr = vm_machine_attribute(task_self(), address, length,
117 MATTR_CACHE, &flush);
118 if (kr != KERN_SUCCESS)
119 mach_error("Could not flush the instruction cache", kr);
124 os_protect(vm_address_t address, vm_size_t length, vm_prot_t protection)
126 vm_protect(task_self(), address, length, FALSE, protection);
138 if (segments == -1) {
144 (task_self(), &addr, &size, &bullshit, &bullshit, &bullshit,
145 &bullshit, &bullshit, &bullshit) != KERN_SUCCESS)
149 && addr_map[curseg - 1].start + addr_map[curseg - 1].length ==
150 addr) addr_map[curseg - 1].length += size;
152 addr_map[curseg].start = addr;
153 addr_map[curseg].length = size;
163 for (curseg = 0; curseg < segments; curseg++)
164 if (addr_map[curseg].start <= test
165 && test < addr_map[curseg].start + addr_map[curseg].length)
172 sigbus_handler(int signal, int code, struct sigcontext *context)
174 if (!interrupt_maybe_gc(signal, code, context))
175 interrupt_handle_now(signal, code, context);
180 os_install_interrupt_handlers(void)
183 interrupt_install_low_level_handler(SIGBUS, sigbus_handler);
186 interrupt_install_low_level_handler(SIGSEGV, sigbus_handler);