ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/Linux/paranoia.cpp
Revision: 1.1
Committed: 2002-02-04T16:58:13Z (22 years, 10 months ago) by cebix
Branch: MAIN
Branch point for: cebix
Log Message:
Initial revision

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * paranoia.cpp - Check undocumented features of the Linux kernel that
3     * SheepShaver relies upon
4     *
5     * SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig
6     *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with this program; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20     */
21    
22     #include <unistd.h>
23     #include <stdio.h>
24     #include <signal.h>
25     #include <errno.h>
26    
27     #include "sysdeps.h"
28     #include "main.h"
29     #include "user_strings.h"
30    
31     typedef struct {
32     uint32 u[4];
33     } __attribute((aligned(16))) vector128;
34     #include <linux/elf.h>
35    
36     #define DEBUG 1
37     #include "debug.h"
38    
39    
40     // Constants
41     const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack
42    
43     // Prototypes
44     extern "C" void *get_sp(void);
45     extern "C" void set_r2(uint32 val);
46     extern void paranoia_check(void);
47     static void sigusr2_handler(int sig, sigcontext_struct *sc);
48    
49     // Global variables
50     static void *sig_stack = NULL;
51    
52     static int err = 0;
53     static void *sig_sp = NULL;
54     static void *sig_r4 = NULL;
55     static int sig_sc_signal = 0;
56     static void *sig_sc_regs = NULL;
57     static uint32 sig_r2 = 0;
58    
59    
60     void paranoia_check(void)
61     {
62     char str[256];
63    
64     D(bug("Paranoia checks...\n"));
65    
66     // Create and install stack for signal handler
67     sig_stack = malloc(SIG_STACK_SIZE);
68     if (sig_stack == NULL) {
69     ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
70     exit(1);
71     }
72    
73     struct sigaltstack old_stack;
74     struct sigaltstack new_stack;
75     new_stack.ss_sp = sig_stack;
76     new_stack.ss_flags = 0;
77     new_stack.ss_size = SIG_STACK_SIZE;
78     if (sigaltstack(&new_stack, &old_stack) < 0) {
79     sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
80     ErrorAlert(str);
81     exit(1);
82     }
83    
84     // Install SIGUSR2 signal handler
85     static struct sigaction old_action;
86     static struct sigaction sigusr2_action;
87     sigemptyset(&sigusr2_action.sa_mask);
88     sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler;
89     sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART;
90     sigusr2_action.sa_restorer = NULL;
91     if (sigaction(SIGUSR2, &sigusr2_action, &old_action) < 0) {
92     sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno));
93     ErrorAlert(str);
94     exit(1);
95     }
96    
97     // Raise SIGUSR2
98     set_r2(0xaffebad5);
99     raise(SIGUSR2);
100    
101     // Check error code
102     switch (err) {
103     case 1:
104     printf("FATAL: sigaltstack() doesn't seem to work (sp in signal handler was %08lx, expected %08lx..%08lx)\n", (uint32)sig_sp, (uint32)sig_stack, (uint32)sig_stack + SIG_STACK_SIZE);
105     break;
106     case 2:
107     printf("FATAL: r4 in signal handler (%08lx) doesn't point to stack\n", (uint32)sig_r4);
108     break;
109     case 3:
110     printf("FATAL: r4 in signal handler doesn't seem to point to a sigcontext_struct (signal number was %d, expected %d)", sig_sc_signal, SIGUSR2);
111     break;
112     case 4:
113     printf("FATAL: sc->regs in signal handler (%08lx) doesn't point to stack\n", (uint32)sig_sc_regs);
114     break;
115     case 5:
116     printf("FATAL: sc->regs->gpr[2] in signal handler (%08lx) doesn't have expected value (%08x)\n", (uint32)sig_r2, 0xaffebad5);
117     break;
118     }
119     if (err) {
120     printf("Maybe you need a different kernel?\n");
121     exit(1);
122     }
123    
124     // Clean up
125     D(bug("...passed\n"));
126     sigaction(SIGUSR2, &old_action, NULL);
127     sigaltstack(&old_stack, NULL);
128     free(sig_stack);
129     }
130    
131    
132     static void sigusr2_handler(int sig, sigcontext_struct *sc)
133     {
134     // Check whether sigaltstack works
135     sig_sp = get_sp();
136     if (sig_sp < sig_stack || sig_sp >= ((uint8 *)sig_stack + SIG_STACK_SIZE)) {
137     err = 1;
138     return;
139     }
140    
141     // Check whether r4 points to info on the stack
142     sig_r4 = sc;
143     if (sig_r4 < sig_stack || sig_r4 >= ((uint8 *)sig_stack + SIG_STACK_SIZE)) {
144     err = 2;
145     return;
146     }
147    
148     // Check whether r4 looks like a sigcontext
149     sig_sc_signal = sc->signal;
150     if (sig_sc_signal != SIGUSR2) {
151     err = 3;
152     return;
153     }
154    
155     // Check whether sc->regs points to info on the stack
156     sig_sc_regs = sc->regs;
157     if (sig_sc_regs < sig_stack || sig_sc_regs >= ((uint8 *)sig_stack + SIG_STACK_SIZE)) {
158     err = 4;
159     return;
160     }
161    
162     // Check whether r2 still holds the value we set it to
163     sig_r2 = sc->regs->gpr[2];
164     if (sig_r2 != 0xaffebad5) {
165     err = 5;
166     return;
167     }
168     }