ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/main_unix.cpp
(Generate patch)

Comparing SheepShaver/src/Unix/main_unix.cpp (file contents):
Revision 1.44 by gbeauche, 2004-06-26T16:25:22Z vs.
Revision 1.70 by gbeauche, 2005-07-06T04:58:34Z

# Line 1 | Line 1
1   /*
2   *  main_unix.cpp - Emulation core, Unix implementation
3   *
4 < *  SheepShaver (C) 1997-2004 Christian Bauer and Marc Hellwig
4 > *  SheepShaver (C) 1997-2005 Christian Bauer and Marc Hellwig
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 27 | Line 27
27   *  is slightly different from the SysV ABI used by Linux:
28   *    - Stack frames are different (e.g. LR is stored in 8(r1) under
29   *      MacOS, but in 4(r1) under Linux)
30 < *    - There is no TOC under Linux; r2 is free for the user
30 > *    - There is a pointer to Thread Local Storage (TLS) under Linux with
31 > *      recent enough glibc. This is r2 in 32-bit mode and r13 in
32 > *      64-bit mode (PowerOpen/AIX ABI)
33   *    - r13 is used as a small data pointer under Linux (but appearently
34   *      it is not used this way? To be sure, we specify -msdata=none
35   *      in the Makefile)
36 < *    - As there is no TOC, there are also no TVECTs under Linux;
37 < *      function pointers point directly to the function code
36 > *    - There are no TVECTs under Linux; function pointers point
37 > *      directly to the function code
38   *  The Execute*() functions have to account for this. Additionally, we
39   *  cannot simply call MacOS functions by getting their TVECT and jumping
40   *  to it. Such calls are done via the call_macos*() functions in
# Line 65 | Line 67
67   *  ExecutePPC (or any function that might cause a mode switch). The signal
68   *  stack is restored before exiting the SIGUSR2 handler.
69   *
70 < *  There is apparently another problem when processing signals. In
71 < *  fullscreen mode, we get quick updates of the mouse position. This
72 < *  causes an increased number of calls to TriggerInterrupt(). And,
73 < *  since IRQ_NEST is not fully handled atomically, nested calls to
74 < *  ppc_interrupt() may cause stack corruption to eventually crash the
75 < *  emulator.
74 < *
75 < *  FIXME:
76 < *  The current solution is to allocate another signal stack when
77 < *  processing ppc_interrupt(). However, it may be better to detect
78 < *  the INTFLAG_ADB case and handle it specifically with some extra mutex?
70 > *  Note that POSIX standard says you can't modify the alternate
71 > *  signal stack while the process is executing on it. There is a
72 > *  hackaround though: we install a trampoline SIGUSR2 handler that
73 > *  sets up an alternate stack itself and calls the real handler.
74 > *  Then, when we call sigaltstack() there, we no longer get an EPERM,
75 > *  i.e. it now works.
76   *
77   *  TODO:
78   *    check if SIGSEGV handler works for all registers (including FP!)
# Line 105 | Line 102
102   #include "xpram.h"
103   #include "timer.h"
104   #include "adb.h"
108 #include "sony.h"
109 #include "disk.h"
110 #include "cdrom.h"
111 #include "scsi.h"
105   #include "video.h"
113 #include "audio.h"
114 #include "ether.h"
115 #include "serial.h"
116 #include "clip.h"
117 #include "extfs.h"
106   #include "sys.h"
107   #include "macos_util.h"
108   #include "rom_patches.h"
109   #include "user_strings.h"
110   #include "vm_alloc.h"
111   #include "sigsegv.h"
112 < #include "thunks.h"
112 > #include "sigregs.h"
113  
114   #define DEBUG 0
115   #include "debug.h"
116  
117  
118 + #ifdef HAVE_DIRENT_H
119 + #include <dirent.h>
120 + #endif
121 +
122   #ifdef USE_SDL
123   #include <SDL.h>
124   #endif
# Line 162 | Line 154
154   // Interrupts in native mode?
155   #define INTERRUPTS_IN_NATIVE_MODE 1
156  
165 // Number of alternate stacks for signal handlers?
166 #define SIG_STACK_COUNT 4
167
157  
158   // Constants
159   const char ROM_FILE_NAME[] = "ROM";
160   const char ROM_FILE_NAME2[] = "Mac OS ROM";
161  
162 + #if REAL_ADDRESSING
163   const uintptr RAM_BASE = 0x20000000;            // Base address of RAM
164 < const uint32 SIG_STACK_SIZE = 0x10000;          // Size of signal stack
165 <
166 <
177 < #if !EMULATED_PPC
178 < struct sigregs {
179 <        uint32 nip;
180 <        uint32 link;
181 <        uint32 ctr;
182 <        uint32 msr;
183 <        uint32 xer;
184 <        uint32 ccr;
185 <        uint32 gpr[32];
186 < };
187 <
188 < #if defined(__linux__)
189 < #include <sys/ucontext.h>
190 < #define MACHINE_REGISTERS(scp)  ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext.regs))
191 <
192 < struct machine_regs : public pt_regs
193 < {
194 <        u_long & cr()                           { return pt_regs::ccr; }
195 <        uint32 cr() const                       { return pt_regs::ccr; }
196 <        uint32 lr() const                       { return pt_regs::link; }
197 <        uint32 ctr() const                      { return pt_regs::ctr; }
198 <        uint32 xer() const                      { return pt_regs::xer; }
199 <        uint32 msr() const                      { return pt_regs::msr; }
200 <        uint32 dar() const                      { return pt_regs::dar; }
201 <        u_long & pc()                           { return pt_regs::nip; }
202 <        uint32 pc() const                       { return pt_regs::nip; }
203 <        u_long & gpr(int i)                     { return pt_regs::gpr[i]; }
204 <        uint32 gpr(int i) const         { return pt_regs::gpr[i]; }
205 < };
206 < #endif
207 <
208 < #if defined(__APPLE__) && defined(__MACH__)
209 < #include <sys/signal.h>
210 < extern "C" int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss);
211 <
212 < #include <sys/ucontext.h>
213 < #define MACHINE_REGISTERS(scp)  ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext))
214 <
215 < struct machine_regs : public mcontext
216 < {
217 <        uint32 & cr()                           { return ss.cr; }
218 <        uint32 cr() const                       { return ss.cr; }
219 <        uint32 lr() const                       { return ss.lr; }
220 <        uint32 ctr() const                      { return ss.ctr; }
221 <        uint32 xer() const                      { return ss.xer; }
222 <        uint32 msr() const                      { return ss.srr1; }
223 <        uint32 dar() const                      { return es.dar; }
224 <        uint32 & pc()                           { return ss.srr0; }
225 <        uint32 pc() const                       { return ss.srr0; }
226 <        uint32 & gpr(int i)                     { return (&ss.r0)[i]; }
227 <        uint32 gpr(int i) const         { return (&ss.r0)[i]; }
228 < };
229 < #endif
230 <
231 < static void build_sigregs(sigregs *srp, machine_regs *mrp)
232 < {
233 <        srp->nip = mrp->pc();
234 <        srp->link = mrp->lr();
235 <        srp->ctr = mrp->ctr();
236 <        srp->msr = mrp->msr();
237 <        srp->xer = mrp->xer();
238 <        srp->ccr = mrp->cr();
239 <        for (int i = 0; i < 32; i++)
240 <                srp->gpr[i] = mrp->gpr(i);
241 < }
242 <
243 < static struct sigaltstack sig_stacks[SIG_STACK_COUNT];  // Stacks for signal handlers
244 < static int sig_stack_id = 0;                                                    // Stack slot currently used
245 <
246 < static inline void sig_stack_acquire(void)
247 < {
248 <        if (++sig_stack_id == SIG_STACK_COUNT) {
249 <                printf("FATAL: signal stack overflow\n");
250 <                return;
251 <        }
252 <        sigaltstack(&sig_stacks[sig_stack_id], NULL);
253 < }
254 <
255 < static inline void sig_stack_release(void)
256 < {
257 <        if (--sig_stack_id < 0) {
258 <                printf("FATAL: signal stack underflow\n");
259 <                return;
260 <        }
261 <        sigaltstack(&sig_stacks[sig_stack_id], NULL);
262 < }
164 > #else
165 > // FIXME: needs to be >= 0x04000000
166 > const uintptr RAM_BASE = 0x10000000;            // Base address of RAM
167   #endif
168 + const uint32 SIG_STACK_SIZE = 0x10000;          // Size of signal stack
169  
170  
171   // Global variables (exported)
172   #if !EMULATED_PPC
173 < void *TOC;                              // Small data pointer (r13)
173 > void *TOC = NULL;               // Pointer to Thread Local Storage (r2)
174 > void *R13 = NULL;               // Pointer to .sdata section (r13 under Linux)
175   #endif
176   uint32 RAMBase;                 // Base address of Mac RAM
177   uint32 RAMSize;                 // Size of Mac RAM
# Line 275 | Line 181 | uint32 DRCacheAddr;            // Address of DR Ca
181   uint32 PVR;                             // Theoretical PVR
182   int64 CPUClockSpeed;    // Processor clock speed (Hz)
183   int64 BusClockSpeed;    // Bus clock speed (Hz)
184 + int64 TimebaseSpeed;    // Timebase clock speed (Hz)
185 + uint8 *RAMBaseHost;             // Base address of Mac RAM (host address space)
186 + uint8 *ROMBaseHost;             // Base address of Mac ROM (host address space)
187  
188  
189   // Global variables
# Line 315 | Line 224 | static uintptr sig_stack = 0;                          // Stac
224   #else
225   static struct sigaction sigsegv_action;         // Data access exception signal (of emulator thread)
226   static struct sigaction sigill_action;          // Illegal instruction signal (of emulator thread)
227 + static struct sigaltstack sig_stack;            // Stack for signal handlers
228 + static struct sigaltstack extra_stack;          // Stack for SIGSEGV inside interrupt handler
229   static bool emul_thread_fatal = false;          // Flag: MacOS thread crashed, tick thread shall dump debug output
230   static sigregs sigsegv_regs;                            // Register dump when crashed
231   static const char *crash_reason = NULL;         // Reason of the crash (SIGSEGV, SIGBUS, SIGILL)
# Line 323 | Line 234 | static const char *crash_reason = NULL;
234   uint32  SheepMem::page_size;                            // Size of a native page
235   uintptr SheepMem::zero_page = 0;                        // Address of ro page filled in with zeros
236   uintptr SheepMem::base = 0x60000000;            // Address of SheepShaver data
237 < uintptr SheepMem::top = 0;                                      // Top of SheepShaver data (stack like storage)
237 > uintptr SheepMem::proc;                                         // Bottom address of SheepShave procedures
238 > uintptr SheepMem::data;                                         // Top of SheepShaver data (stack like storage)
239  
240  
241   // Prototypes
242 + static bool kernel_data_init(void);
243 + static void kernel_data_exit(void);
244   static void Quit(void);
245   static void *emul_func(void *arg);
246   static void *nvram_func(void *arg);
# Line 337 | Line 251 | extern void init_emul_ppc(void);
251   extern void exit_emul_ppc(void);
252   sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
253   #else
254 < static void sigusr2_handler(int sig, siginfo_t *sip, void *scp);
254 > extern "C" void sigusr2_handler_init(int sig, siginfo_t *sip, void *scp);
255 > extern "C" void sigusr2_handler(int sig, siginfo_t *sip, void *scp);
256   static void sigsegv_handler(int sig, siginfo_t *sip, void *scp);
257   static void sigill_handler(int sig, siginfo_t *sip, void *scp);
258   #endif
# Line 345 | Line 260 | static void sigill_handler(int sig, sigi
260  
261   // From asm_linux.S
262   #if !EMULATED_PPC
348 extern "C" void *get_toc(void);
263   extern "C" void *get_sp(void);
264 < extern "C" void flush_icache_range(void *start, void *end);
264 > extern "C" void *get_r2(void);
265 > extern "C" void set_r2(void *);
266 > extern "C" void *get_r13(void);
267 > extern "C" void set_r13(void *);
268 > extern "C" void flush_icache_range(uint32 start, uint32 end);
269   extern "C" void jump_to_rom(uint32 entry, uint32 context);
270   extern "C" void quit_emulator(void);
271   extern "C" void execute_68k(uint32 pc, M68kRegisters *r);
# Line 411 | Line 329 | int atomic_or(int *var, int v)
329  
330  
331   /*
332 + *  Memory management helpers
333 + */
334 +
335 + static inline int vm_mac_acquire(uint32 addr, uint32 size)
336 + {
337 +        return vm_acquire_fixed(Mac2HostAddr(addr), size);
338 + }
339 +
340 + static inline int vm_mac_release(uint32 addr, uint32 size)
341 + {
342 +        return vm_release(Mac2HostAddr(addr), size);
343 + }
344 +
345 +
346 + /*
347   *  Main program
348   */
349  
# Line 426 | Line 359 | static void usage(const char *prg_name)
359   int main(int argc, char **argv)
360   {
361          char str[256];
429        uint32 *boot_globs;
430        int16 i16;
362          int rom_fd;
363          FILE *proc_file;
364          const char *rom_path;
# Line 444 | Line 375 | int main(int argc, char **argv)
375          printf(" %s\n", GetString(STR_ABOUT_TEXT2));
376  
377   #if !EMULATED_PPC
378 + #ifdef SYSTEM_CLOBBERS_R2
379          // Get TOC pointer
380 <        TOC = get_toc();
380 >        TOC = get_r2();
381 > #endif
382 > #ifdef SYSTEM_CLOBBERS_R13
383 >        // Get r13 register
384 >        R13 = get_r13();
385 > #endif
386   #endif
387  
388   #ifdef ENABLE_GTK
# Line 479 | Line 416 | int main(int argc, char **argv)
416   #ifdef USE_SDL_VIDEO
417          sdl_flags |= SDL_INIT_VIDEO;
418   #endif
419 + #ifdef USE_SDL_AUDIO
420 +        sdl_flags |= SDL_INIT_AUDIO;
421 + #endif
422          assert(sdl_flags != 0);
423          if (SDL_Init(sdl_flags) == -1) {
424                  char str[256];
# Line 512 | Line 452 | int main(int argc, char **argv)
452  
453   #if !EMULATED_PPC
454          // Create and install stacks for signal handlers
455 <        for (int i = 0; i < SIG_STACK_COUNT; i++) {
456 <                void *sig_stack = malloc(SIG_STACK_SIZE);
457 <                D(bug("Signal stack %d at %p\n", i, sig_stack));
458 <                if (sig_stack == NULL) {
459 <                        ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
520 <                        goto quit;
521 <                }
522 <                sig_stacks[i].ss_sp = sig_stack;
523 <                sig_stacks[i].ss_flags = 0;
524 <                sig_stacks[i].ss_size = SIG_STACK_SIZE;
455 >        sig_stack.ss_sp = malloc(SIG_STACK_SIZE);
456 >        D(bug("Signal stack at %p\n", sig_stack.ss_sp));
457 >        if (sig_stack.ss_sp == NULL) {
458 >                ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
459 >                goto quit;
460          }
461 <        sig_stack_id = 0;
462 <        if (sigaltstack(&sig_stacks[0], NULL) < 0) {
461 >        sig_stack.ss_flags = 0;
462 >        sig_stack.ss_size = SIG_STACK_SIZE;
463 >        if (sigaltstack(&sig_stack, NULL) < 0) {
464                  sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
465                  ErrorAlert(str);
466                  goto quit;
467          }
468 +        extra_stack.ss_sp = malloc(SIG_STACK_SIZE);
469 +        D(bug("Extra stack at %p\n", extra_stack.ss_sp));
470 +        if (extra_stack.ss_sp == NULL) {
471 +                ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
472 +                goto quit;
473 +        }
474 +        extra_stack.ss_flags = 0;
475 +        extra_stack.ss_size = SIG_STACK_SIZE;
476   #endif
477  
478   #if !EMULATED_PPC
# Line 566 | Line 510 | int main(int argc, char **argv)
510          PVR = 0x00040000;                       // Default: 604
511          CPUClockSpeed = 100000000;      // Default: 100MHz
512          BusClockSpeed = 100000000;      // Default: 100MHz
513 +        TimebaseSpeed =  25000000;      // Default:  25MHz
514   #if EMULATED_PPC
515          PVR = 0x000c0000;                       // Default: 7400 (with AltiVec)
516   #elif defined(__APPLE__) && defined(__MACH__)
# Line 592 | Line 537 | int main(int argc, char **argv)
537                                          CPUClockSpeed = value;
538                                  else if (sscanf(line, "%[ |]\"bus-frequency\" = <%x>", head, &value) == 2)
539                                          BusClockSpeed = value;
540 +                                else if (sscanf(line, "%[ |]\"timebase-frequency\" = <%x>", head, &value) == 2)
541 +                                        TimebaseSpeed = value;
542                                  else if (strchr(line, '}'))
543                                          powerpc_node = false;
544                          }
# Line 604 | Line 551 | int main(int argc, char **argv)
551   #else
552          proc_file = fopen("/proc/cpuinfo", "r");
553          if (proc_file) {
554 +                // CPU specs from Linux kernel
555 +                // TODO: make it more generic with features (e.g. AltiVec) and
556 +                // cache information and friends for NameRegistry
557 +                static const struct {
558 +                        uint32 pvr_mask;
559 +                        uint32 pvr_value;
560 +                        const char *cpu_name;
561 +                }
562 +                cpu_specs[] = {
563 +                        { 0xffff0000, 0x00010000, "601" },
564 +                        { 0xffff0000, 0x00030000, "603" },
565 +                        { 0xffff0000, 0x00060000, "603e" },
566 +                        { 0xffff0000, 0x00070000, "603ev" },
567 +                        { 0xffff0000, 0x00040000, "604" },
568 +                        { 0xfffff000, 0x00090000, "604e" },
569 +                        { 0xffff0000, 0x00090000, "604r" },
570 +                        { 0xffff0000, 0x000a0000, "604ev" },
571 +                        { 0xffffffff, 0x00084202, "740/750" },
572 +                        { 0xfffff000, 0x00083000, "745/755" },
573 +                        { 0xfffffff0, 0x00080100, "750CX" },
574 +                        { 0xfffffff0, 0x00082200, "750CX" },
575 +                        { 0xfffffff0, 0x00082210, "750CXe" },
576 +                        { 0xffffff00, 0x70000100, "750FX" },
577 +                        { 0xffffffff, 0x70000200, "750FX" },
578 +                        { 0xffff0000, 0x70000000, "750FX" },
579 +                        { 0xffff0000, 0x70020000, "750GX" },
580 +                        { 0xffff0000, 0x00080000, "740/750" },
581 +                        { 0xffffffff, 0x000c1101, "7400 (1.1)" },
582 +                        { 0xffff0000, 0x000c0000, "7400" },
583 +                        { 0xffff0000, 0x800c0000, "7410" },
584 +                        { 0xffffffff, 0x80000200, "7450" },
585 +                        { 0xffffffff, 0x80000201, "7450" },
586 +                        { 0xffff0000, 0x80000000, "7450" },
587 +                        { 0xffffff00, 0x80010100, "7455" },
588 +                        { 0xffffffff, 0x80010200, "7455" },
589 +                        { 0xffff0000, 0x80010000, "7455" },
590 +                        { 0xffff0000, 0x80020000, "7457" },
591 +                        { 0xffff0000, 0x80030000, "7447A" },
592 +                        { 0x7fff0000, 0x00810000, "82xx" },
593 +                        { 0x7fff0000, 0x00820000, "8280" },
594 +                        { 0xffff0000, 0x00400000, "Power3 (630)" },
595 +                        { 0xffff0000, 0x00410000, "Power3 (630+)" },
596 +                        { 0xffff0000, 0x00360000, "I-star" },
597 +                        { 0xffff0000, 0x00370000, "S-star" },
598 +                        { 0xffff0000, 0x00350000, "Power4" },
599 +                        { 0xffff0000, 0x00390000, "PPC970" },
600 +                        { 0xffff0000, 0x003a0000, "POWER5" },
601 +                        { 0, 0, 0 }
602 +                };
603 +
604                  char line[256];
605                  while(fgets(line, 255, proc_file)) {
606                          // Read line
# Line 616 | Line 613 | int main(int argc, char **argv)
613                          int i;
614                          char value[256];
615                          if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) {
616 <                                if (strcmp(value, "601") == 0)
617 <                                        PVR = 0x00010000;
618 <                                else if (strcmp(value, "603") == 0)
619 <                                        PVR = 0x00030000;
620 <                                else if (strcmp(value, "604") == 0)
621 <                                        PVR = 0x00040000;
622 <                                else if (strcmp(value, "603e") == 0)
623 <                                        PVR = 0x00060000;
624 <                                else if (strcmp(value, "603ev") == 0)
625 <                                        PVR = 0x00070000;
629 <                                else if (strcmp(value, "604e") == 0)
630 <                                        PVR = 0x00090000;
631 <                                else if (strcmp(value, "604ev5") == 0)
632 <                                        PVR = 0x000a0000;
633 <                                else if (strcmp(value, "750") == 0)
634 <                                        PVR = 0x00080000;
635 <                                else if (strcmp(value, "821") == 0)
636 <                                        PVR = 0x00320000;
637 <                                else if (strcmp(value, "860") == 0)
638 <                                        PVR = 0x00500000;
639 <                                else if (strcmp(value, "7400") == 0)
640 <                                        PVR = 0x000c0000;
641 <                                else if (strcmp(value, "7410") == 0)
642 <                                        PVR = 0x800c0000;
643 <                                else
616 >                                // Search by name
617 >                                const char *cpu_name = NULL;
618 >                                for (int i = 0; cpu_specs[i].pvr_mask != 0; i++) {
619 >                                        if (strcmp(cpu_specs[i].cpu_name, value) == 0) {
620 >                                                cpu_name = cpu_specs[i].cpu_name;
621 >                                                PVR = cpu_specs[i].pvr_value;
622 >                                                break;
623 >                                        }
624 >                                }
625 >                                if (cpu_name == NULL)
626                                          printf("WARNING: Unknown CPU type '%s', assuming 604\n", value);
627 +                                else
628 +                                        printf("Found a PowerPC %s processor\n", cpu_name);
629                          }
630                          if (sscanf(line, "clock : %dMHz", &i) == 1)
631                                  CPUClockSpeed = BusClockSpeed = i * 1000000;
# Line 660 | Line 644 | int main(int argc, char **argv)
644                          BusClockSpeed = value.l;
645                  fclose(proc_file);
646          }
647 +
648 +        // Get actual timebase frequency
649 +        TimebaseSpeed = BusClockSpeed / 4;
650 +        DIR *cpus_dir;
651 +        if ((cpus_dir = opendir("/proc/device-tree/cpus")) != NULL) {
652 +                struct dirent *cpu_entry;
653 +                while ((cpu_entry = readdir(cpus_dir)) != NULL) {
654 +                        if (strstr(cpu_entry->d_name, "PowerPC,") == cpu_entry->d_name) {
655 +                                char timebase_freq_node[256];
656 +                                sprintf(timebase_freq_node, "/proc/device-tree/cpus/%s/timebase-frequency", cpu_entry->d_name);
657 +                                proc_file = fopen(timebase_freq_node, "r");
658 +                                if (proc_file) {
659 +                                        union { uint8 b[4]; uint32 l; } value;
660 +                                        if (fread(value.b, sizeof(value), 1, proc_file) == 1)
661 +                                                TimebaseSpeed = value.l;
662 +                                        fclose(proc_file);
663 +                                }
664 +                        }
665 +                }
666 +                closedir(cpus_dir);
667 +        }
668   #endif
669 +        // Remap any newer G4/G5 processor to plain G4 for compatibility
670 +        switch (PVR >> 16) {
671 +        case 0x8000:                            // 7450
672 +        case 0x8001:                            // 7455
673 +        case 0x8002:                            // 7457
674 +        case 0x8003:                            // 7447A
675 +        case 0x0039:                            //  970
676 +                PVR = 0x000c0000;               // 7400
677 +                break;
678 +        }
679          D(bug("PVR: %08x (assumed)\n", PVR));
680  
681          // Init system routines
# Line 686 | Line 701 | int main(int argc, char **argv)
701  
702   #ifndef PAGEZERO_HACK
703          // Create Low Memory area (0x0000..0x3000)
704 <        if (vm_acquire_fixed((char *)0, 0x3000) < 0) {
704 >        if (vm_mac_acquire(0, 0x3000) < 0) {
705                  sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
706                  ErrorAlert(str);
707                  goto quit;
# Line 695 | Line 710 | int main(int argc, char **argv)
710   #endif
711  
712          // Create areas for Kernel Data
713 <        kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600);
699 <        if (kernel_area == -1) {
700 <                sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno));
701 <                ErrorAlert(str);
702 <                goto quit;
703 <        }
704 <        if (shmat(kernel_area, (void *)KERNEL_DATA_BASE, 0) < 0) {
705 <                sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno));
706 <                ErrorAlert(str);
713 >        if (!kernel_data_init())
714                  goto quit;
715 <        }
709 <        if (shmat(kernel_area, (void *)KERNEL_DATA2_BASE, 0) < 0) {
710 <                sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno));
711 <                ErrorAlert(str);
712 <                goto quit;
713 <        }
714 <        kernel_data = (KernelData *)KERNEL_DATA_BASE;
715 >        kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE);
716          emulator_data = &kernel_data->ed;
717          KernelDataAddr = KERNEL_DATA_BASE;
718 <        D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data));
718 >        D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE));
719 >        D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed)));
720  
721          // Create area for DR Cache
722 <        if (vm_acquire_fixed((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) {
722 >        if (vm_mac_acquire(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) {
723                  sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno));
724                  ErrorAlert(str);
725                  goto quit;
726          }
727          dr_emulator_area_mapped = true;
728 <        if (vm_acquire_fixed((void *)DR_CACHE_BASE, DR_CACHE_SIZE) < 0) {
728 >        if (vm_mac_acquire(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) {
729                  sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno));
730                  ErrorAlert(str);
731                  goto quit;
# Line 747 | Line 749 | int main(int argc, char **argv)
749          }
750  
751          // Create area for Mac ROM
752 <        if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) {
752 >        if (vm_mac_acquire(ROM_BASE, ROM_AREA_SIZE) < 0) {
753                  sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
754                  ErrorAlert(str);
755                  goto quit;
756          }
757 +        ROMBaseHost = Mac2HostAddr(ROM_BASE);
758   #if !EMULATED_PPC
759 <        if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
759 >        if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
760                  sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
761                  ErrorAlert(str);
762                  goto quit;
763          }
764   #endif
765          rom_area_mapped = true;
766 <        D(bug("ROM area at %08x\n", ROM_BASE));
766 >        D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROM_BASE));
767  
768          // Create area for Mac RAM
769          RAMSize = PrefsFindInt32("ramsize");
# Line 769 | Line 772 | int main(int argc, char **argv)
772                  RAMSize = 8*1024*1024;
773          }
774  
775 <        if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) {
775 >        if (vm_mac_acquire(RAM_BASE, RAMSize) < 0) {
776                  sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
777                  ErrorAlert(str);
778                  goto quit;
779          }
780 +        RAMBaseHost = Mac2HostAddr(RAM_BASE);
781   #if !EMULATED_PPC
782 <        if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
782 >        if (vm_protect(RAMBaseHost, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
783                  sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
784                  ErrorAlert(str);
785                  goto quit;
# Line 783 | Line 787 | int main(int argc, char **argv)
787   #endif
788          RAMBase = RAM_BASE;
789          ram_area_mapped = true;
790 <        D(bug("RAM area at %08x\n", RAMBase));
790 >        D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase));
791  
792          if (RAMBase > ROM_BASE) {
793                  ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR));
# Line 819 | Line 823 | int main(int argc, char **argv)
823          }
824          delete[] rom_tmp;
825  
826 <        // Load NVRAM
827 <        XPRAMInit();
824 <
825 <        // Load XPRAM default values if signature not found
826 <        if (XPRAM[0x130c] != 0x4e || XPRAM[0x130d] != 0x75
827 <         || XPRAM[0x130e] != 0x4d || XPRAM[0x130f] != 0x63) {
828 <                D(bug("Loading XPRAM default values\n"));
829 <                memset(XPRAM + 0x1300, 0, 0x100);
830 <                XPRAM[0x130c] = 0x4e;   // "NuMc" signature
831 <                XPRAM[0x130d] = 0x75;
832 <                XPRAM[0x130e] = 0x4d;
833 <                XPRAM[0x130f] = 0x63;
834 <                XPRAM[0x1301] = 0x80;   // InternalWaitFlags = DynWait (don't wait for SCSI devices upon bootup)
835 <                XPRAM[0x1310] = 0xa8;   // Standard PRAM values
836 <                XPRAM[0x1311] = 0x00;
837 <                XPRAM[0x1312] = 0x00;
838 <                XPRAM[0x1313] = 0x22;
839 <                XPRAM[0x1314] = 0xcc;
840 <                XPRAM[0x1315] = 0x0a;
841 <                XPRAM[0x1316] = 0xcc;
842 <                XPRAM[0x1317] = 0x0a;
843 <                XPRAM[0x131c] = 0x00;
844 <                XPRAM[0x131d] = 0x02;
845 <                XPRAM[0x131e] = 0x63;
846 <                XPRAM[0x131f] = 0x00;
847 <                XPRAM[0x1308] = 0x13;
848 <                XPRAM[0x1309] = 0x88;
849 <                XPRAM[0x130a] = 0x00;
850 <                XPRAM[0x130b] = 0xcc;
851 <                XPRAM[0x1376] = 0x00;   // OSDefault = MacOS
852 <                XPRAM[0x1377] = 0x01;
853 <        }
854 <
855 <        // Set boot volume
856 <        i16 = PrefsFindInt32("bootdrive");
857 <        XPRAM[0x1378] = i16 >> 8;
858 <        XPRAM[0x1379] = i16 & 0xff;
859 <        i16 = PrefsFindInt32("bootdriver");
860 <        XPRAM[0x137a] = i16 >> 8;
861 <        XPRAM[0x137b] = i16 & 0xff;
862 <
863 <        // Create BootGlobs at top of Mac memory
864 <        memset((void *)(RAMBase + RAMSize - 4096), 0, 4096);
865 <        BootGlobsAddr = RAMBase + RAMSize - 0x1c;
866 <        boot_globs = (uint32 *)BootGlobsAddr;
867 <        boot_globs[-5] = htonl(RAMBase + RAMSize);      // MemTop
868 <        boot_globs[0] = htonl(RAMBase);                         // First RAM bank
869 <        boot_globs[1] = htonl(RAMSize);
870 <        boot_globs[2] = htonl((uint32)-1);                      // End of bank table
871 <
872 <        // Init thunks
873 <        if (!ThunksInit())
874 <                goto quit;
875 <
876 <        // Init drivers
877 <        SonyInit();
878 <        DiskInit();
879 <        CDROMInit();
880 <        SCSIInit();
881 <
882 <        // Init external file system
883 <        ExtFSInit();
884 <
885 <        // Init ADB
886 <        ADBInit();
887 <
888 <        // Init audio
889 <        AudioInit();
890 <
891 <        // Init network
892 <        EtherInit();
893 <
894 <        // Init serial ports
895 <        SerialInit();
896 <
897 <        // Init Time Manager
898 <        TimerInit();
899 <
900 <        // Init clipboard
901 <        ClipInit();
902 <
903 <        // Init video
904 <        if (!VideoInit())
905 <                goto quit;
906 <
907 <        // Install ROM patches
908 <        if (!PatchROM()) {
909 <                ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR));
826 >        // Initialize everything
827 >        if (!InitAll())
828                  goto quit;
829 <        }
829 >        D(bug("Initialization complete\n"));
830  
831          // Clear caches (as we loaded and patched code) and write protect ROM
832   #if !EMULATED_PPC
833 <        MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE);
916 < #endif
917 <        vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE);
918 <
919 <        // Initialize Kernel Data
920 <        memset(kernel_data, 0, sizeof(KernelData));
921 <        if (ROMType == ROMTYPE_NEWWORLD) {
922 <                uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32));
923 <                memset((void *)of_dev_tree, 0, 4 * sizeof(uint32));
924 <                uintptr vector_lookup_tbl = SheepMem::Reserve(128);
925 <                uintptr vector_mask_tbl = SheepMem::Reserve(64);
926 <                memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80);
927 <                memset((void *)vector_lookup_tbl, 0, 128);
928 <                memset((void *)vector_mask_tbl, 0, 64);
929 <                kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE);
930 <                kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree);                        // OF device tree base
931 <                kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl);
932 <                kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl);
933 <                kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE);                           // OpenPIC base
934 <                kernel_data->v[0xbb0 >> 2] = htonl(0);                                          // ADB base
935 <                kernel_data->v[0xc20 >> 2] = htonl(RAMSize);
936 <                kernel_data->v[0xc24 >> 2] = htonl(RAMSize);
937 <                kernel_data->v[0xc30 >> 2] = htonl(RAMSize);
938 <                kernel_data->v[0xc34 >> 2] = htonl(RAMSize);
939 <                kernel_data->v[0xc38 >> 2] = htonl(0x00010020);
940 <                kernel_data->v[0xc3c >> 2] = htonl(0x00200001);
941 <                kernel_data->v[0xc40 >> 2] = htonl(0x00010000);
942 <                kernel_data->v[0xc50 >> 2] = htonl(RAMBase);
943 <                kernel_data->v[0xc54 >> 2] = htonl(RAMSize);
944 <                kernel_data->v[0xf60 >> 2] = htonl(PVR);
945 <                kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed);                      // clock-frequency
946 <                kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed);                      // bus-frequency
947 <                kernel_data->v[0xf6c >> 2] = htonl(BusClockSpeed / 4);          // timebase-frequency
948 <        } else {
949 <                kernel_data->v[0xc80 >> 2] = htonl(RAMSize);
950 <                kernel_data->v[0xc84 >> 2] = htonl(RAMSize);
951 <                kernel_data->v[0xc90 >> 2] = htonl(RAMSize);
952 <                kernel_data->v[0xc94 >> 2] = htonl(RAMSize);
953 <                kernel_data->v[0xc98 >> 2] = htonl(0x00010020);
954 <                kernel_data->v[0xc9c >> 2] = htonl(0x00200001);
955 <                kernel_data->v[0xca0 >> 2] = htonl(0x00010000);
956 <                kernel_data->v[0xcb0 >> 2] = htonl(RAMBase);
957 <                kernel_data->v[0xcb4 >> 2] = htonl(RAMSize);
958 <                kernel_data->v[0xf80 >> 2] = htonl(PVR);
959 <                kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed);                      // clock-frequency
960 <                kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed);                      // bus-frequency
961 <                kernel_data->v[0xf8c >> 2] = htonl(BusClockSpeed / 4);          // timebase-frequency
962 <        }
963 <
964 <        // Initialize extra low memory
965 <        D(bug("Initializing Low Memory...\n"));
966 <        memset(NULL, 0, 0x3000);
967 <        WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h'));                  // Signature to detect SheepShaver
968 <        WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr);                                 // For trap replacement routines
969 <        WriteMacInt32(XLM_PVR, PVR);                                                                    // Theoretical PVR
970 <        WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed);                                    // For DriverServicesLib patch
971 <        WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN);                // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode)
972 <        WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage());                             // Pointer to read-only page with all bits set to 0
973 < #if !EMULATED_PPC
974 <        WriteMacInt32(XLM_TOC, (uint32)TOC);                                                            // TOC pointer of emulator
833 >        flush_icache_range(ROM_BASE, ROM_BASE + ROM_AREA_SIZE);
834   #endif
835 <        WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT));       // DLPI ethernet driver functions
977 <        WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM));
978 <        WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN));
979 <        WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE));
980 <        WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT));
981 <        WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV));
982 <        WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO));
983 <        D(bug("Low Memory initialized\n"));
835 >        vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE);
836  
837          // Start 60Hz thread
838          tick_thread_cancel = false;
# Line 1012 | Line 864 | int main(int argc, char **argv)
864   #if !EMULATED_PPC
865          // Install interrupt signal handler
866          sigemptyset(&sigusr2_action.sa_mask);
867 <        sigusr2_action.sa_sigaction = sigusr2_handler;
867 >        sigusr2_action.sa_sigaction = sigusr2_handler_init;
868          sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO;
869   #ifdef HAVE_SIGNAL_SA_RESTORER
870          sigusr2_action.sa_restorer = NULL;
# Line 1075 | Line 927 | static void Quit(void)
927          sigaction(SIGILL, &sigill_action, NULL);
928  
929          // Delete stacks for signal handlers
930 <        for (int i = 0; i < SIG_STACK_COUNT; i++) {
931 <                void *sig_stack = sig_stacks[i].ss_sp;
932 <                if (sig_stack)
933 <                        free(sig_stack);
1082 <        }
930 >        if (sig_stack.ss_sp)
931 >                free(sig_stack.ss_sp);
932 >        if (extra_stack.ss_sp)
933 >                free(extra_stack.ss_sp);
934   #endif
935  
936 <        // Save NVRAM
937 <        XPRAMExit();
1087 <
1088 <        // Exit clipboard
1089 <        ClipExit();
1090 <
1091 <        // Exit Time Manager
1092 <        TimerExit();
1093 <
1094 <        // Exit serial
1095 <        SerialExit();
1096 <
1097 <        // Exit network
1098 <        EtherExit();
1099 <
1100 <        // Exit audio
1101 <        AudioExit();
1102 <
1103 <        // Exit ADB
1104 <        ADBExit();
1105 <
1106 <        // Exit video
1107 <        VideoExit();
1108 <
1109 <        // Exit external file system
1110 <        ExtFSExit();
1111 <
1112 <        // Exit drivers
1113 <        SCSIExit();
1114 <        CDROMExit();
1115 <        DiskExit();
1116 <        SonyExit();
1117 <
1118 <        // Delete thunks
1119 <        ThunksExit();
936 >        // Deinitialize everything
937 >        ExitAll();
938  
939          // Delete SheepShaver globals
940          SheepMem::Exit();
941  
942          // Delete RAM area
943          if (ram_area_mapped)
944 <                vm_release((char *)RAM_BASE, RAMSize);
944 >                vm_mac_release(RAM_BASE, RAMSize);
945  
946          // Delete ROM area
947          if (rom_area_mapped)
948 <                vm_release((char *)ROM_BASE, ROM_AREA_SIZE);
948 >                vm_mac_release(ROM_BASE, ROM_AREA_SIZE);
949  
950          // Delete DR cache areas
951          if (dr_emulator_area_mapped)
952 <                vm_release((void *)DR_EMULATOR_BASE, DR_EMULATOR_SIZE);
952 >                vm_mac_release(DR_EMULATOR_BASE, DR_EMULATOR_SIZE);
953          if (dr_cache_area_mapped)
954 <                vm_release((void *)DR_CACHE_BASE, DR_CACHE_SIZE);
954 >                vm_mac_release(DR_CACHE_BASE, DR_CACHE_SIZE);
955  
956          // Delete Kernel Data area
957 <        if (kernel_area >= 0) {
1140 <                shmdt((void *)KERNEL_DATA_BASE);
1141 <                shmdt((void *)KERNEL_DATA2_BASE);
1142 <                shmctl(kernel_area, IPC_RMID, NULL);
1143 <        }
957 >        kernel_data_exit();
958  
959          // Delete Low Memory area
960          if (lm_area_mapped)
961 <                munmap((char *)0x0000, 0x3000);
961 >                vm_mac_release(0, 0x3000);
962  
963          // Close /dev/zero
964          if (zero_fd > 0)
# Line 1172 | Line 986 | static void Quit(void)
986  
987  
988   /*
989 + *  Initialize Kernel Data segments
990 + */
991 +
992 + #if defined(__CYGWIN__)
993 + #define WIN32_LEAN_AND_MEAN
994 + #include <windows.h>
995 +
996 + static HANDLE kernel_handle;                            // Shared memory handle for Kernel Data
997 + static DWORD allocation_granule;                        // Minimum size of allocateable are (64K)
998 + static DWORD kernel_area_size;                          // Size of Kernel Data area
999 + #endif
1000 +
1001 + static bool kernel_data_init(void)
1002 + {
1003 +        char str[256];
1004 + #ifdef _WIN32
1005 +        SYSTEM_INFO si;
1006 +        GetSystemInfo(&si);
1007 +        allocation_granule = si.dwAllocationGranularity;
1008 +        kernel_area_size = (KERNEL_AREA_SIZE + allocation_granule - 1) & -allocation_granule;
1009 +
1010 +        char rcs[10];
1011 +        LPVOID kernel_addr;
1012 +        kernel_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, kernel_area_size, NULL);
1013 +        if (kernel_handle == NULL) {
1014 +                sprintf(rcs, "%d", GetLastError());
1015 +                sprintf(str, GetString(STR_KD_SHMGET_ERR), rcs);
1016 +                ErrorAlert(str);
1017 +                return false;
1018 +        }
1019 +        kernel_addr = (LPVOID)Mac2HostAddr(KERNEL_DATA_BASE & -allocation_granule);
1020 +        if (MapViewOfFileEx(kernel_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, kernel_area_size, kernel_addr) != kernel_addr) {
1021 +                sprintf(rcs, "%d", GetLastError());
1022 +                sprintf(str, GetString(STR_KD_SHMAT_ERR), rcs);
1023 +                ErrorAlert(str);
1024 +                return false;
1025 +        }
1026 +        kernel_addr = (LPVOID)Mac2HostAddr(KERNEL_DATA2_BASE & -allocation_granule);
1027 +        if (MapViewOfFileEx(kernel_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, kernel_area_size, kernel_addr) != kernel_addr) {
1028 +                sprintf(rcs, "%d", GetLastError());
1029 +                sprintf(str, GetString(STR_KD2_SHMAT_ERR), rcs);
1030 +                ErrorAlert(str);
1031 +                return false;
1032 +        }
1033 + #else
1034 +        kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600);
1035 +        if (kernel_area == -1) {
1036 +                sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno));
1037 +                ErrorAlert(str);
1038 +                return false;
1039 +        }
1040 +        if (shmat(kernel_area, Mac2HostAddr(KERNEL_DATA_BASE), 0) < 0) {
1041 +                sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno));
1042 +                ErrorAlert(str);
1043 +                return false;
1044 +        }
1045 +        if (shmat(kernel_area, Mac2HostAddr(KERNEL_DATA2_BASE), 0) < 0) {
1046 +                sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno));
1047 +                ErrorAlert(str);
1048 +                return false;
1049 +        }
1050 + #endif
1051 +        return true;
1052 + }
1053 +
1054 +
1055 + /*
1056 + *  Deallocate Kernel Data segments
1057 + */
1058 +
1059 + static void kernel_data_exit(void)
1060 + {
1061 + #ifdef _WIN32
1062 +        if (kernel_handle) {
1063 +                UnmapViewOfFile(Mac2HostAddr(KERNEL_DATA_BASE & -allocation_granule));
1064 +                UnmapViewOfFile(Mac2HostAddr(KERNEL_DATA2_BASE & -allocation_granule));
1065 +                CloseHandle(kernel_handle);
1066 +        }
1067 + #else
1068 +        if (kernel_area >= 0) {
1069 +                shmdt(Mac2HostAddr(KERNEL_DATA_BASE));
1070 +                shmdt(Mac2HostAddr(KERNEL_DATA2_BASE));
1071 +                shmctl(kernel_area, IPC_RMID, NULL);
1072 +        }
1073 + #endif
1074 + }
1075 +
1076 +
1077 + /*
1078   *  Jump into Mac ROM, start 680x0 emulator
1079   */
1080  
# Line 1258 | Line 1161 | void QuitEmulator(void)
1161  
1162  
1163   /*
1261 *  Pause/resume emulator
1262 */
1263
1264 void PauseEmulator(void)
1265 {
1266        pthread_kill(emul_thread, SIGSTOP);
1267 }
1268
1269 void ResumeEmulator(void)
1270 {
1271        pthread_kill(emul_thread, SIGCONT);
1272 }
1273
1274
1275 /*
1164   *  Dump 68k registers
1165   */
1166  
# Line 1300 | Line 1188 | void Dump68kRegs(M68kRegisters *r)
1188   *  Make code executable
1189   */
1190  
1191 < void MakeExecutable(int dummy, void *start, uint32 length)
1191 > void MakeExecutable(int dummy, uint32 start, uint32 length)
1192   {
1193 <        if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE)))
1193 >        if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE)))
1194                  return;
1195   #if EMULATED_PPC
1196 <        FlushCodeCache((uintptr)start, (uintptr)start + length);
1196 >        FlushCodeCache(start, start + length);
1197   #else
1198 <        flush_icache_range(start, (void *)((uintptr)start + length));
1198 >        flush_icache_range(start, start + length);
1199   #endif
1200   }
1201  
1202  
1203   /*
1316 *  Patch things after system startup (gets called by disk driver accRun routine)
1317 */
1318
1319 void PatchAfterStartup(void)
1320 {
1321        ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL);
1322        InstallExtFS();
1323 }
1324
1325
1326 /*
1204   *  NVRAM watchdog thread (saves NVRAM every minute)
1205   */
1206  
# Line 1426 | Line 1303 | static void *tick_func(void *arg)
1303          }
1304  
1305          uint64 end = GetTicks_usec();
1306 <        D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1306 >        D(bug("%lld ticks in %lld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1307          return NULL;
1308   }
1309  
# Line 1476 | Line 1353 | struct B2_mutex {
1353              pthread_mutexattr_init(&attr);
1354              // Initialize the mutex for priority inheritance --
1355              // required for accurate timing.
1356 < #ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL
1356 > #if defined(HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL) && !defined(__CYGWIN__)
1357              pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
1358   #endif
1359   #if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL)
# Line 1550 | Line 1427 | void B2_delete_mutex(B2_mutex *mutex)
1427   #if !EMULATED_PPC
1428   void TriggerInterrupt(void)
1429   {
1430 <        if (ready_for_signals)
1430 >        if (ready_for_signals) {
1431 >                idle_resume();
1432                  pthread_kill(emul_thread, SIGUSR2);
1433 +        }
1434   }
1435   #endif
1436  
# Line 1606 | Line 1485 | void EnableInterrupt(void)
1485   */
1486  
1487   #if !EMULATED_PPC
1488 < static void sigusr2_handler(int sig, siginfo_t *sip, void *scp)
1488 > void sigusr2_handler(int sig, siginfo_t *sip, void *scp)
1489   {
1490          machine_regs *r = MACHINE_REGISTERS(scp);
1491  
1492 + #ifdef SYSTEM_CLOBBERS_R2
1493 +        // Restore pointer to Thread Local Storage
1494 +        set_r2(TOC);
1495 + #endif
1496 + #ifdef SYSTEM_CLOBBERS_R13
1497 +        // Restore pointer to .sdata section
1498 +        set_r13(R13);
1499 + #endif
1500 +
1501   #ifdef USE_SDL_VIDEO
1502          // We must fill in the events queue in the same thread that did call SDL_SetVideoMode()
1503          SDL_PumpEvents();
# Line 1635 | Line 1523 | static void sigusr2_handler(int sig, sig
1523                          // 68k emulator inactive, in nanokernel?
1524                          if (r->gpr(1) != KernelDataAddr) {
1525  
1526 <                                // Set extra stack for nested interrupts
1527 <                                sig_stack_acquire();
1526 >                                // Set extra stack for SIGSEGV handler
1527 >                                sigaltstack(&extra_stack, NULL);
1528                                  
1529                                  // Prepare for 68k interrupt level 1
1530                                  WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1);
# Line 1649 | Line 1537 | static void sigusr2_handler(int sig, sig
1537                                  else
1538                                          ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr);
1539  
1540 <                                // Reset normal signal stack
1541 <                                sig_stack_release();
1540 >                                // Reset normal stack
1541 >                                sigaltstack(&sig_stack, NULL);
1542                          }
1543                          break;
1544   #endif
# Line 1661 | Line 1549 | static void sigusr2_handler(int sig, sig
1549                          if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
1550  
1551                                  // Set extra stack for SIGSEGV handler
1552 <                                sig_stack_acquire();
1552 >                                sigaltstack(&extra_stack, NULL);
1553   #if 1
1554                                  // Execute full 68k interrupt routine
1555                                  M68kRegisters r;
# Line 1687 | Line 1575 | static void sigusr2_handler(int sig, sig
1575                                          }
1576                                  }
1577   #endif
1578 <                                // Reset normal signal stack
1579 <                                sig_stack_release();
1578 >                                // Reset normal stack
1579 >                                sigaltstack(&sig_stack, NULL);
1580                          }
1581                          break;
1582   #endif
# Line 1709 | Line 1597 | static void sigsegv_handler(int sig, sig
1597          // Get effective address
1598          uint32 addr = r->dar();
1599          
1600 + #ifdef SYSTEM_CLOBBERS_R2
1601 +        // Restore pointer to Thread Local Storage
1602 +        set_r2(TOC);
1603 + #endif
1604 + #ifdef SYSTEM_CLOBBERS_R13
1605 +        // Restore pointer to .sdata section
1606 +        set_r13(R13);
1607 + #endif
1608 +
1609   #if ENABLE_VOSF
1610          // Handle screen fault.
1611          extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction);
# Line 1958 | Line 1855 | static void sigill_handler(int sig, sigi
1855          machine_regs *r = MACHINE_REGISTERS(scp);
1856          char str[256];
1857  
1858 + #ifdef SYSTEM_CLOBBERS_R2
1859 +        // Restore pointer to Thread Local Storage
1860 +        set_r2(TOC);
1861 + #endif
1862 + #ifdef SYSTEM_CLOBBERS_R13
1863 +        // Restore pointer to .sdata section
1864 +        set_r13(R13);
1865 + #endif
1866 +
1867          // Fault in Mac ROM or RAM?
1868          bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize));
1869          if (mac_fault) {
# Line 2126 | Line 2032 | bool SheepMem::Init(void)
2032          page_size = getpagesize();
2033  
2034          // Allocate SheepShaver globals
2035 <        if (vm_acquire_fixed((char *)base, size) < 0)
2035 >        proc = base;
2036 >        if (vm_mac_acquire(base, size) < 0)
2037                  return false;
2038  
2039 <        // Allocate page with all bits set to 0
2040 <        zero_page = base + size;
2041 <        if (vm_acquire_fixed((char *)zero_page, page_size) < 0)
2042 <                return false;
2043 <        memset((char *)zero_page, 0, page_size);
2137 <        if (vm_protect((char *)zero_page, page_size, VM_PAGE_READ) < 0)
2039 >        // Allocate page with all bits set to 0, right in the middle
2040 >        // This is also used to catch undesired overlaps between proc and data areas
2041 >        zero_page = proc + (size / 2);
2042 >        Mac_memset(zero_page, 0, page_size);
2043 >        if (vm_protect(Mac2HostAddr(zero_page), page_size, VM_PAGE_READ) < 0)
2044                  return false;
2045  
2046   #if EMULATED_PPC
2047          // Allocate alternate stack for PowerPC interrupt routine
2048 <        sig_stack = zero_page + page_size;
2049 <        if (vm_acquire_fixed((char *)sig_stack, SIG_STACK_SIZE) < 0)
2048 >        sig_stack = base + size;
2049 >        if (vm_mac_acquire(sig_stack, SIG_STACK_SIZE) < 0)
2050                  return false;
2051   #endif
2052  
2053 <        top = base + size;
2053 >        data = base + size;
2054          return true;
2055   }
2056  
2057   void SheepMem::Exit(void)
2058   {
2059 <        if (top) {
2059 >        if (data) {
2060                  // Delete SheepShaver globals
2061 <                vm_release((void *)base, size);
2156 <
2157 <                // Delete zero page
2158 <                vm_release((void *)zero_page, page_size);
2061 >                vm_mac_release(base, size);
2062  
2063   #if EMULATED_PPC
2064                  // Delete alternate stack for PowerPC interrupt routine
2065 <                vm_release((void *)sig_stack, SIG_STACK_SIZE);
2065 >                vm_mac_release(sig_stack, SIG_STACK_SIZE);
2066   #endif
2067          }
2068   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines