Dynamic space relocation, part 1 of 2
[sbcl.git] / src / runtime / validate.c
blob1e3fd08ea395ce6ba07ce22fbc2f70e3afc691db
1 /*
2 * memory validation
3 */
5 /*
6 * This software is part of the SBCL system. See the README file for
7 * more information.
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
16 #include <stdio.h>
17 #include <stdlib.h>
19 #include "sbcl.h"
20 #include "runtime.h"
21 #include "os.h"
22 #include "globals.h"
23 #include "interr.h"
24 #include "validate.h"
25 #include "interr.h" /* for declaration of lose */
27 #ifdef LISP_FEATURE_RELOCATABLE_HEAP
28 uword_t DYNAMIC_SPACE_START;
29 #endif
31 static void
32 ensure_space(lispobj *start, uword_t size)
34 if (os_validate(NOT_MOVABLE, (os_vm_address_t)start, (os_vm_size_t)size)==NULL) {
35 fprintf(stderr,
36 "ensure_space: failed to allocate %lu bytes at %p\n",
37 (long unsigned)size, start);
38 fprintf(stderr,
39 "(hint: Try \"ulimit -a\"; maybe you should increase memory limits.)\n");
40 exit(1);
44 os_vm_address_t undefined_alien_address = 0;
46 static void
47 ensure_undefined_alien(void) {
48 os_vm_address_t start = os_allocate(os_vm_page_size);
49 if (start) {
50 os_protect(start, os_vm_page_size, OS_VM_PROT_NONE);
51 undefined_alien_address = start;
52 } else {
53 lose("could not allocate guard page for undefined alien\n");
57 void
58 allocate_spaces(void)
60 #ifdef PRINTNOISE
61 printf("allocating memory ...");
62 fflush(stdout);
63 #endif
64 #ifndef LISP_FEATURE_RELOCATABLE_HEAP
65 // Allocate the largest space(s) first,
66 // since if that fails, it's game over.
67 #ifdef LISP_FEATURE_GENCGC
68 ensure_space( (lispobj *)DYNAMIC_SPACE_START , dynamic_space_size);
69 #else
70 ensure_space( (lispobj *)DYNAMIC_0_SPACE_START, dynamic_space_size);
71 ensure_space( (lispobj *)DYNAMIC_1_SPACE_START, dynamic_space_size);
72 #endif
73 #endif
75 ensure_space( (lispobj *)READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE);
76 ensure_space( (lispobj *)STATIC_SPACE_START , STATIC_SPACE_SIZE);
77 #ifdef LISP_FEATURE_LINKAGE_TABLE
78 ensure_space( (lispobj *)LINKAGE_TABLE_SPACE_START,
79 LINKAGE_TABLE_SPACE_SIZE);
80 #endif
81 #if defined(IMMOBILE_SPACE_START) /*&& !defined(LISP_FEATURE_RELOCATABLE_HEAP)*/
82 ensure_space((lispobj *)IMMOBILE_SPACE_START, IMMOBILE_SPACE_SIZE);
83 #endif
85 #ifdef LISP_FEATURE_OS_PROVIDES_DLOPEN
86 ensure_undefined_alien();
87 #endif
89 #ifdef PRINTNOISE
90 printf(" done.\n");
91 #endif
94 static inline void
95 protect_page(void *page, int protect_p, os_vm_prot_t flags) {
96 os_protect(page, os_vm_page_size, protect_p ?
97 flags : OS_VM_PROT_ALL);
100 #define DEF_PROTECT_PAGE(name,page_name,flags) \
101 void \
102 protect_##name(int protect_p, struct thread *thread) { \
103 if (!thread) \
104 thread = arch_os_get_current_thread(); \
105 protect_page(page_name(thread), protect_p, flags); \
108 DEF_PROTECT_PAGE(control_stack_hard_guard_page,
109 CONTROL_STACK_HARD_GUARD_PAGE,
110 OS_VM_PROT_NONE)
111 DEF_PROTECT_PAGE(control_stack_guard_page,
112 CONTROL_STACK_GUARD_PAGE,
113 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE)
114 DEF_PROTECT_PAGE(control_stack_return_guard_page,
115 CONTROL_STACK_RETURN_GUARD_PAGE,
116 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE)
118 DEF_PROTECT_PAGE(binding_stack_hard_guard_page,
119 BINDING_STACK_HARD_GUARD_PAGE,
120 OS_VM_PROT_NONE)
121 DEF_PROTECT_PAGE(binding_stack_guard_page,
122 BINDING_STACK_GUARD_PAGE,
123 OS_VM_PROT_NONE)
124 DEF_PROTECT_PAGE(binding_stack_return_guard_page,
125 BINDING_STACK_RETURN_GUARD_PAGE,
126 OS_VM_PROT_NONE)
128 DEF_PROTECT_PAGE(alien_stack_hard_guard_page,
129 ALIEN_STACK_HARD_GUARD_PAGE,
130 OS_VM_PROT_NONE)
131 DEF_PROTECT_PAGE(alien_stack_guard_page,
132 ALIEN_STACK_GUARD_PAGE,
133 OS_VM_PROT_NONE)
134 DEF_PROTECT_PAGE(alien_stack_return_guard_page,
135 ALIEN_STACK_RETURN_GUARD_PAGE,
136 OS_VM_PROT_NONE)