[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / runtime / storage / mprotect.lc
1 %
2 % (c) The AQUA Project, Glasgow University, 1995
3 %
4 %************************************************************************
5 %*                                                                      *
6 \section[mprotect.lc]{Memory Protection}
7 %*                                                                      *
8 %************************************************************************
9
10 Is @mprotect@ POSIX now? [Yup, POSIX.4 -- sof]
11
12 \begin{code}
13 #if STACK_CHECK_BY_PAGE_FAULT
14
15 /* #define STK_CHK_DEBUG */
16
17 #include "rtsdefs.h"
18
19 # ifdef HAVE_SYS_TYPES_H
20 #  include <sys/types.h>
21 # endif
22
23 # ifdef HAVE_SYS_MMAN_H
24 #  include <sys/mman.h>
25 # endif
26
27 # if defined(_SC_PAGE_SIZE) && !defined(_SC_PAGESIZE)
28     /* Death to HP-UX.  What are standards for, anyway??? */
29 #  define _SC_PAGESIZE _SC_PAGE_SIZE
30 # endif
31
32 # if defined(_SC_PAGESIZE)
33 #  define GETPAGESIZE() sysconf(_SC_PAGESIZE)
34 # else
35 #  if defined(HAVE_GETPAGESIZE)
36 #   define GETPAGESIZE()    getpagesize()
37 #  else
38 #   if defined(linux_TARGET_OS) || defined(linuxaout_TARGET_OS)
39 #    /* it has it, but it is in BSD land; easier to just say so */
40 #    define GETPAGESIZE()   getpagesize()
41 #   else 
42 #    error getpagesize
43 #   endif
44 #  endif
45 # endif
46
47 #if defined(sunos4_TARGET_OS)
48 int getpagesize PROTO((void));
49 int mprotect PROTO((caddr_t, size_t, int));
50 #endif
51
52 #if defined(aix_TARGET_OS)
53 /* PROT_NONE doesn't work on aix, PROT_READ works and should suit the job */
54 #define PROT_NONE PROT_READ
55 #endif
56  
57 /* Needed for FreeBSD (SDM, 96/03) */
58 #ifndef PROT_NONE
59 #define PROT_NONE 0
60 #endif
61
62 /* For VirtualProtect() and its flags */
63 #if defined(cygwin32_TARGET_OS)
64 #include <windows.h>
65 #endif
66
67 void 
68 unmapMiddleStackPage(addr, size)
69 char * /*caddr_t*/ addr;
70 int size;
71 {
72     int pagesize = GETPAGESIZE();
73     char * middle = (char *) (((W_) (addr + size / 2)) / pagesize * pagesize);
74 #if defined(cygwin32_TARGET_OS)
75     unsigned int old_prot;
76 #endif
77
78 # ifdef STK_CHK_DEBUG
79     fprintf(stderr, "pagesize: %x\nstack start: %08lx\nstack size: %08lx\nstack middle: %08lx\n",
80       pagesize, addr, size, middle);
81 # endif
82
83     if (middle < addr || middle + pagesize > addr + size) {
84         fprintf(stderr, "Stack too small; stack overflow trap disabled.\n");
85         return;
86     }
87 /* mprotect() is broken in beta18, so we use the native Win32
88    call instead
89 */
90 #if defined(cygwin32_TARGET_OS)
91     if (VirtualProtect(middle, pagesize, PAGE_NOACCESS, &old_prot) == 0) {
92 #else
93     if (mprotect(middle, pagesize, PROT_NONE) == -1) {
94 #endif
95         perror("mprotect");
96         EXIT(EXIT_FAILURE);
97     }
98     if (install_segv_handler()) {
99         fprintf(stderr, "Can't install SIGSEGV handler for stack overflow check.\n");
100         EXIT(EXIT_FAILURE);
101     }
102 }
103
104 #endif /* STACK_CHECK_BY_PAGE_FAULT */
105 \end{code}