efe727363dedec75ad799f922abe5d670d264df6
[projects/cmucl/cmucl.git] / src / lisp / validate.c
1 /*
2  * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/validate.c,v 1.25 2007/07/09 16:04:12 fgilham Rel $
3  *
4  * Memory Validation
5  */
6
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #ifdef sparc
12 #include <alloca.h>
13 #endif
14
15 #include "lisp.h"
16 #include "os.h"
17 #include "globals.h"
18 #include "validate.h"
19 #include "internals.h"
20
21 #ifdef sparc
22 extern void make_holes(void);
23 #endif
24
25 static void
26 ensure_space(lispobj * start, size_t size)
27 {
28     if (os_validate((os_vm_address_t) start, size) == NULL) {
29         fprintf(stderr,
30                 "ensure_space: Failed to validate %ld bytes at 0x%08lx\n",
31                 (unsigned long) size, (unsigned long) start);
32         exit(1);
33     }
34 }
35
36
37 /* builtin_image_flag is used as a flag indicating that the lisp image is
38    built into the executable.  The other variables are set to actual values
39    in elf.c when the core section is mapped. FMG
40 */
41 extern int builtin_image_flag;
42 long image_dynamic_space_size = 0;
43 long image_read_only_space_size = 0;
44 long image_static_space_size = 0;
45
46 void
47 validate(void)
48 {
49     /* void *dynamic_space_data = NULL; */
50
51     /* Read-Only Space */
52     read_only_space = (lispobj *) READ_ONLY_SPACE_START;
53     /* Note that if the lisp core is not built into the image,
54        the below expression will be equal to this:
55        ensure_space(read_only_space, READ_ONLY_SPACE_SIZE);
56        FMG
57     */
58     ensure_space((lispobj *)((int)read_only_space + image_read_only_space_size),
59                  read_only_space_size - image_read_only_space_size);
60
61     /* Static Space */
62     static_space = (lispobj *) STATIC_SPACE_START;
63     /* Note that if the lisp core is not built into the image,
64        the below expression will be equal to this:
65        ensure_space(static_space, STATIC_SPACE_SIZE);
66        FMG
67     */
68     ensure_space((lispobj *)((int)static_space + image_static_space_size),
69                  static_space_size - image_static_space_size);
70
71     /* Dynamic-0 Space */
72     dynamic_0_space = (lispobj *) DYNAMIC_0_SPACE_START;
73     /* Note that if the lisp core is not built into the image,
74        the below expression will be equal to this:
75        ensure_space(dynamic_0_space, dynamic_space_size);
76        FMG
77     */
78     ensure_space((lispobj *)((int)dynamic_0_space + image_dynamic_space_size),
79                  dynamic_space_size - image_dynamic_space_size);
80
81     current_dynamic_space = dynamic_0_space;
82
83 #ifndef GENCGC
84     /* Dynamic-1 Space */
85     dynamic_1_space = (lispobj *) DYNAMIC_1_SPACE_START;
86     ensure_space(dynamic_1_space, dynamic_space_size);
87     /* I'm not sure about the following, or if the lisp executable
88        stuff will work with a garbage collector other than gencgc.
89        FMG
90     */
91     /*
92       if (builtin_image_flag != 0)
93       dynamic_space_data = alloca((int) (&image_dynamic_space_size));
94     */
95 #endif
96
97     /* Control Stack */
98     control_stack = (lispobj *) CONTROL_STACK_START;
99 #if (defined(i386) || defined(__x86_64))
100     control_stack_end = (lispobj *) (CONTROL_STACK_START + control_stack_size);
101 #endif
102     ensure_space(control_stack, control_stack_size);
103
104 #ifdef SIGNAL_STACK_START
105     ensure_space((lispobj *) SIGNAL_STACK_START, SIGNAL_STACK_SIZE);
106 #endif
107
108     /* Binding Stack */
109     binding_stack = (lispobj *) BINDING_STACK_START;
110     ensure_space(binding_stack, binding_stack_size);
111 #ifdef LINKAGE_TABLE
112     ensure_space((lispobj *) FOREIGN_LINKAGE_SPACE_START,
113                  FOREIGN_LINKAGE_SPACE_SIZE);
114 #endif
115 #ifdef sparc
116     make_holes();
117 #endif
118
119 #ifdef PRINTNOISE
120     printf(" done.\n");
121 #endif
122
123 #ifdef RED_ZONE_HIT
124     os_guard_control_stack(0, 1);
125 #endif
126 }