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.52 by gbeauche, 2004-11-13T14:09:15Z vs.
Revision 1.78 by asvitkine, 2007-08-27T21:45:13Z

# 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 > #include "rpc.h"
114  
115   #define DEBUG 0
116   #include "debug.h"
# Line 166 | Line 155
155   // Interrupts in native mode?
156   #define INTERRUPTS_IN_NATIVE_MODE 1
157  
169 // Number of alternate stacks for signal handlers?
170 #define SIG_STACK_COUNT 4
171
158  
159   // Constants
160   const char ROM_FILE_NAME[] = "ROM";
# Line 183 | Line 169 | const uintptr RAM_BASE = 0x10000000;           //
169   const uint32 SIG_STACK_SIZE = 0x10000;          // Size of signal stack
170  
171  
186 #if !EMULATED_PPC
187 struct sigregs {
188        uint32 nip;
189        uint32 link;
190        uint32 ctr;
191        uint32 msr;
192        uint32 xer;
193        uint32 ccr;
194        uint32 gpr[32];
195 };
196
197 #if defined(__linux__)
198 #include <sys/ucontext.h>
199 #define MACHINE_REGISTERS(scp)  ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext.regs))
200
201 struct machine_regs : public pt_regs
202 {
203        u_long & cr()                           { return pt_regs::ccr; }
204        uint32 cr() const                       { return pt_regs::ccr; }
205        uint32 lr() const                       { return pt_regs::link; }
206        uint32 ctr() const                      { return pt_regs::ctr; }
207        uint32 xer() const                      { return pt_regs::xer; }
208        uint32 msr() const                      { return pt_regs::msr; }
209        uint32 dar() const                      { return pt_regs::dar; }
210        u_long & pc()                           { return pt_regs::nip; }
211        uint32 pc() const                       { return pt_regs::nip; }
212        u_long & gpr(int i)                     { return pt_regs::gpr[i]; }
213        uint32 gpr(int i) const         { return pt_regs::gpr[i]; }
214 };
215 #endif
216
217 #if defined(__APPLE__) && defined(__MACH__)
218 #include <sys/signal.h>
219 extern "C" int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss);
220
221 #include <sys/ucontext.h>
222 #define MACHINE_REGISTERS(scp)  ((machine_regs *)(((ucontext_t *)scp)->uc_mcontext))
223
224 struct machine_regs : public mcontext
225 {
226        uint32 & cr()                           { return ss.cr; }
227        uint32 cr() const                       { return ss.cr; }
228        uint32 lr() const                       { return ss.lr; }
229        uint32 ctr() const                      { return ss.ctr; }
230        uint32 xer() const                      { return ss.xer; }
231        uint32 msr() const                      { return ss.srr1; }
232        uint32 dar() const                      { return es.dar; }
233        uint32 & pc()                           { return ss.srr0; }
234        uint32 pc() const                       { return ss.srr0; }
235        uint32 & gpr(int i)                     { return (&ss.r0)[i]; }
236        uint32 gpr(int i) const         { return (&ss.r0)[i]; }
237 };
238 #endif
239
240 static void build_sigregs(sigregs *srp, machine_regs *mrp)
241 {
242        srp->nip = mrp->pc();
243        srp->link = mrp->lr();
244        srp->ctr = mrp->ctr();
245        srp->msr = mrp->msr();
246        srp->xer = mrp->xer();
247        srp->ccr = mrp->cr();
248        for (int i = 0; i < 32; i++)
249                srp->gpr[i] = mrp->gpr(i);
250 }
251
252 static struct sigaltstack sig_stacks[SIG_STACK_COUNT];  // Stacks for signal handlers
253 static int sig_stack_id = 0;                                                    // Stack slot currently used
254
255 static inline void sig_stack_acquire(void)
256 {
257        if (++sig_stack_id == SIG_STACK_COUNT) {
258                printf("FATAL: signal stack overflow\n");
259                return;
260        }
261        sigaltstack(&sig_stacks[sig_stack_id], NULL);
262 }
263
264 static inline void sig_stack_release(void)
265 {
266        if (--sig_stack_id < 0) {
267                printf("FATAL: signal stack underflow\n");
268                return;
269        }
270        sigaltstack(&sig_stacks[sig_stack_id], NULL);
271 }
272 #endif
273
274
172   // Global variables (exported)
173   #if !EMULATED_PPC
174 < void *TOC;                              // Small data pointer (r13)
174 > void *TOC = NULL;               // Pointer to Thread Local Storage (r2)
175 > void *R13 = NULL;               // Pointer to .sdata section (r13 under Linux)
176   #endif
177   uint32 RAMBase;                 // Base address of Mac RAM
178   uint32 RAMSize;                 // Size of Mac RAM
# Line 327 | Line 225 | static uintptr sig_stack = 0;                          // Stac
225   #else
226   static struct sigaction sigsegv_action;         // Data access exception signal (of emulator thread)
227   static struct sigaction sigill_action;          // Illegal instruction signal (of emulator thread)
228 + static struct sigaltstack sig_stack;            // Stack for signal handlers
229 + static struct sigaltstack extra_stack;          // Stack for SIGSEGV inside interrupt handler
230   static bool emul_thread_fatal = false;          // Flag: MacOS thread crashed, tick thread shall dump debug output
231   static sigregs sigsegv_regs;                            // Register dump when crashed
232   static const char *crash_reason = NULL;         // Reason of the crash (SIGSEGV, SIGBUS, SIGILL)
233   #endif
234  
235 + static rpc_connection_t *gui_connection = NULL; // RPC connection to the GUI
236 + static const char *gui_connection_path = NULL;  // GUI connection identifier
237 +
238   uint32  SheepMem::page_size;                            // Size of a native page
239   uintptr SheepMem::zero_page = 0;                        // Address of ro page filled in with zeros
240   uintptr SheepMem::base = 0x60000000;            // Address of SheepShaver data
241 < uintptr SheepMem::top = 0;                                      // Top of SheepShaver data (stack like storage)
241 > uintptr SheepMem::proc;                                         // Bottom address of SheepShave procedures
242 > uintptr SheepMem::data;                                         // Top of SheepShaver data (stack like storage)
243  
244  
245   // Prototypes
246 + static bool kernel_data_init(void);
247 + static void kernel_data_exit(void);
248   static void Quit(void);
249   static void *emul_func(void *arg);
250   static void *nvram_func(void *arg);
# Line 349 | Line 255 | extern void init_emul_ppc(void);
255   extern void exit_emul_ppc(void);
256   sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
257   #else
258 < static void sigusr2_handler(int sig, siginfo_t *sip, void *scp);
258 > extern "C" void sigusr2_handler_init(int sig, siginfo_t *sip, void *scp);
259 > extern "C" void sigusr2_handler(int sig, siginfo_t *sip, void *scp);
260   static void sigsegv_handler(int sig, siginfo_t *sip, void *scp);
261   static void sigill_handler(int sig, siginfo_t *sip, void *scp);
262   #endif
# Line 357 | Line 264 | static void sigill_handler(int sig, sigi
264  
265   // From asm_linux.S
266   #if !EMULATED_PPC
360 extern "C" void *get_toc(void);
267   extern "C" void *get_sp(void);
268 < extern "C" void flush_icache_range(void *start, void *end);
268 > extern "C" void *get_r2(void);
269 > extern "C" void set_r2(void *);
270 > extern "C" void *get_r13(void);
271 > extern "C" void set_r13(void *);
272 > extern "C" void flush_icache_range(uint32 start, uint32 end);
273   extern "C" void jump_to_rom(uint32 entry, uint32 context);
274   extern "C" void quit_emulator(void);
275   extern "C" void execute_68k(uint32 pc, M68kRegisters *r);
# Line 423 | Line 333 | int atomic_or(int *var, int v)
333  
334  
335   /*
336 + *  Memory management helpers
337 + */
338 +
339 + static inline int vm_mac_acquire(uint32 addr, uint32 size)
340 + {
341 +        return vm_acquire_fixed(Mac2HostAddr(addr), size);
342 + }
343 +
344 + static inline int vm_mac_release(uint32 addr, uint32 size)
345 + {
346 +        return vm_release(Mac2HostAddr(addr), size);
347 + }
348 +
349 +
350 + /*
351   *  Main program
352   */
353  
# Line 438 | Line 363 | static void usage(const char *prg_name)
363   int main(int argc, char **argv)
364   {
365          char str[256];
441        int16 i16;
366          int rom_fd;
367          FILE *proc_file;
368          const char *rom_path;
369          uint32 rom_size, actual;
370          uint8 *rom_tmp;
371          time_t now, expire;
372 +        bool memory_mapped_from_zero;
373 +
374 + #ifdef USE_SDL_VIDEO
375 +        // don't let SDL block the screensaver
376 +        setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
377 + #endif
378  
379          // Initialize variables
380          RAMBase = 0;
# Line 455 | Line 385 | int main(int argc, char **argv)
385          printf(" %s\n", GetString(STR_ABOUT_TEXT2));
386  
387   #if !EMULATED_PPC
388 + #ifdef SYSTEM_CLOBBERS_R2
389          // Get TOC pointer
390 <        TOC = get_toc();
390 >        TOC = get_r2();
391 > #endif
392 > #ifdef SYSTEM_CLOBBERS_R13
393 >        // Get r13 register
394 >        R13 = get_r13();
395   #endif
461
462 #ifdef ENABLE_GTK
463        // Init GTK
464        gtk_set_locale();
465        gtk_init(&argc, &argv);
396   #endif
467
468        // Read preferences
469        PrefsInit(argc, argv);
397  
398          // Parse command line arguments
399          for (int i=1; i<argc; i++) {
# Line 478 | Line 405 | int main(int argc, char **argv)
405                          if (i < argc)
406                                  x_display_name = strdup(argv[i]);
407   #endif
408 <                } else if (argv[i][0] == '-') {
408 >                } else if (strcmp(argv[i], "--gui-connection") == 0) {
409 >                        argv[i++] = NULL;
410 >                        if (i < argc) {
411 >                                gui_connection_path = argv[i];
412 >                                argv[i] = NULL;
413 >                        }
414 >                }
415 >        }
416 >
417 >        // Remove processed arguments
418 >        for (int i=1; i<argc; i++) {
419 >                int k;
420 >                for (k=i; k<argc; k++)
421 >                        if (argv[k] != NULL)
422 >                                break;
423 >                if (k > i) {
424 >                        k -= i;
425 >                        for (int j=i+k; j<argc; j++)
426 >                                argv[j-k] = argv[j];
427 >                        argc -= k;
428 >                }
429 >        }
430 >
431 >        // Connect to the external GUI
432 >        if (gui_connection_path) {
433 >                if ((gui_connection = rpc_init_client(gui_connection_path)) == NULL) {
434 >                        fprintf(stderr, "Failed to initialize RPC client connection to the GUI\n");
435 >                        return 1;
436 >                }
437 >        }
438 >
439 > #ifdef ENABLE_GTK
440 >        if (!gui_connection) {
441 >                // Init GTK
442 >                gtk_set_locale();
443 >                gtk_init(&argc, &argv);
444 >        }
445 > #endif
446 >
447 >        // Read preferences
448 >        PrefsInit(argc, argv);
449 >
450 >        // Any command line arguments left?
451 >        for (int i=1; i<argc; i++) {
452 >                if (argv[i][0] == '-') {
453                          fprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
454                          usage(argv[0]);
455                  }
# Line 501 | Line 472 | int main(int argc, char **argv)
472                  goto quit;
473          }
474          atexit(SDL_Quit);
475 +
476 +        // Don't let SDL catch SIGINT and SIGTERM signals
477 +        signal(SIGINT, SIG_DFL);
478 +        signal(SIGTERM, SIG_DFL);
479   #endif
480  
481   #ifndef USE_SDL_VIDEO
# Line 526 | Line 501 | int main(int argc, char **argv)
501  
502   #if !EMULATED_PPC
503          // Create and install stacks for signal handlers
504 <        for (int i = 0; i < SIG_STACK_COUNT; i++) {
505 <                void *sig_stack = malloc(SIG_STACK_SIZE);
506 <                D(bug("Signal stack %d at %p\n", i, sig_stack));
507 <                if (sig_stack == NULL) {
508 <                        ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
534 <                        goto quit;
535 <                }
536 <                sig_stacks[i].ss_sp = sig_stack;
537 <                sig_stacks[i].ss_flags = 0;
538 <                sig_stacks[i].ss_size = SIG_STACK_SIZE;
504 >        sig_stack.ss_sp = malloc(SIG_STACK_SIZE);
505 >        D(bug("Signal stack at %p\n", sig_stack.ss_sp));
506 >        if (sig_stack.ss_sp == NULL) {
507 >                ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
508 >                goto quit;
509          }
510 <        sig_stack_id = 0;
511 <        if (sigaltstack(&sig_stacks[0], NULL) < 0) {
510 >        sig_stack.ss_flags = 0;
511 >        sig_stack.ss_size = SIG_STACK_SIZE;
512 >        if (sigaltstack(&sig_stack, NULL) < 0) {
513                  sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
514                  ErrorAlert(str);
515                  goto quit;
516          }
517 +        extra_stack.ss_sp = malloc(SIG_STACK_SIZE);
518 +        D(bug("Extra stack at %p\n", extra_stack.ss_sp));
519 +        if (extra_stack.ss_sp == NULL) {
520 +                ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
521 +                goto quit;
522 +        }
523 +        extra_stack.ss_flags = 0;
524 +        extra_stack.ss_size = SIG_STACK_SIZE;
525   #endif
526  
527   #if !EMULATED_PPC
# Line 555 | Line 534 | int main(int argc, char **argv)
534          sigsegv_action.sa_restorer = NULL;
535   #endif
536          if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) {
537 <                sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno));
537 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGSEGV", strerror(errno));
538                  ErrorAlert(str);
539                  goto quit;
540          }
541          if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) {
542 <                sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno));
542 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGBUS", strerror(errno));
543                  ErrorAlert(str);
544                  goto quit;
545          }
546   #else
547          // Install SIGSEGV handler for CPU emulator
548          if (!sigsegv_install_handler(sigsegv_handler)) {
549 <                sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno));
549 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGSEGV", strerror(errno));
550                  ErrorAlert(str);
551                  goto quit;
552          }
# Line 659 | Line 638 | int main(int argc, char **argv)
638                          { 0xffff0000, 0x80010000, "7455" },
639                          { 0xffff0000, 0x80020000, "7457" },
640                          { 0xffff0000, 0x80030000, "7447A" },
641 +                        { 0xffff0000, 0x80040000, "7448" },
642                          { 0x7fff0000, 0x00810000, "82xx" },
643                          { 0x7fff0000, 0x00820000, "8280" },
644                          { 0xffff0000, 0x00400000, "Power3 (630)" },
# Line 667 | Line 647 | int main(int argc, char **argv)
647                          { 0xffff0000, 0x00370000, "S-star" },
648                          { 0xffff0000, 0x00350000, "Power4" },
649                          { 0xffff0000, 0x00390000, "PPC970" },
650 +                        { 0xffff0000, 0x003c0000, "PPC970FX" },
651 +                        { 0xffff0000, 0x003a0000, "POWER5 (gr)" },
652 +                        { 0xffff0000, 0x003b0000, "POWER5+ (gs)" },
653 +                        { 0xffff0000, 0x003e0000, "POWER6" },
654 +                        { 0xffff0000, 0x00700000, "Cell Broadband Engine" },
655 +                        { 0x7fff0000, 0x00900000, "PA6T" },
656                          { 0, 0, 0 }
657                  };
658  
# Line 680 | Line 666 | int main(int argc, char **argv)
666  
667                          // Parse line
668                          int i;
669 +                        float f;
670                          char value[256];
671 <                        if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) {
671 >                        if (sscanf(line, "cpu : %[^,]", value) == 1) {
672                                  // Search by name
673                                  const char *cpu_name = NULL;
674                                  for (int i = 0; cpu_specs[i].pvr_mask != 0; i++) {
# Line 696 | Line 683 | int main(int argc, char **argv)
683                                  else
684                                          printf("Found a PowerPC %s processor\n", cpu_name);
685                          }
686 <                        if (sscanf(line, "clock : %dMHz", &i) == 1)
686 >                        if (sscanf(line, "clock : %fMHz", &f) == 1)
687 >                                CPUClockSpeed = BusClockSpeed = ((int64)f) * 1000000;
688 >                        else if (sscanf(line, "clock : %dMHz", &i) == 1)
689                                  CPUClockSpeed = BusClockSpeed = i * 1000000;
690                  }
691                  fclose(proc_file);
# Line 740 | Line 729 | int main(int argc, char **argv)
729          case 0x8000:                            // 7450
730          case 0x8001:                            // 7455
731          case 0x8002:                            // 7457
732 +        case 0x8003:                            // 7447A
733 +        case 0x8004:                            // 7448
734          case 0x0039:                            //  970
735 +        case 0x003c:                            //  970FX
736                  PVR = 0x000c0000;               // 7400
737                  break;
738          }
# Line 767 | Line 759 | int main(int argc, char **argv)
759                  goto quit;
760          }
761  
770 #ifndef PAGEZERO_HACK
771        // Create Low Memory area (0x0000..0x3000)
772        if (vm_acquire_fixed((char *)NATMEM_OFFSET, 0x3000) < 0) {
773                sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
774                ErrorAlert(str);
775                goto quit;
776        }
777        lm_area_mapped = true;
778 #endif
779
762          // Create areas for Kernel Data
763 <        kernel_area = shmget(IPC_PRIVATE, KERNEL_AREA_SIZE, 0600);
782 <        if (kernel_area == -1) {
783 <                sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno));
784 <                ErrorAlert(str);
785 <                goto quit;
786 <        }
787 <        if (shmat(kernel_area, (void *)(KERNEL_DATA_BASE + NATMEM_OFFSET), 0) < 0) {
788 <                sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno));
789 <                ErrorAlert(str);
790 <                goto quit;
791 <        }
792 <        if (shmat(kernel_area, (void *)(KERNEL_DATA2_BASE + NATMEM_OFFSET), 0) < 0) {
793 <                sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno));
794 <                ErrorAlert(str);
763 >        if (!kernel_data_init())
764                  goto quit;
765 <        }
797 <        kernel_data = (KernelData *)(KERNEL_DATA_BASE + NATMEM_OFFSET);
765 >        kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE);
766          emulator_data = &kernel_data->ed;
767          KernelDataAddr = KERNEL_DATA_BASE;
768          D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE));
769          D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed)));
770  
771          // Create area for DR Cache
772 <        if (vm_acquire_fixed((void *)(DR_EMULATOR_BASE + NATMEM_OFFSET), DR_EMULATOR_SIZE) < 0) {
772 >        if (vm_mac_acquire(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) {
773                  sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno));
774                  ErrorAlert(str);
775                  goto quit;
776          }
777          dr_emulator_area_mapped = true;
778 <        if (vm_acquire_fixed((void *)(DR_CACHE_BASE + NATMEM_OFFSET), DR_CACHE_SIZE) < 0) {
778 >        if (vm_mac_acquire(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) {
779                  sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno));
780                  ErrorAlert(str);
781                  goto quit;
# Line 831 | Line 799 | int main(int argc, char **argv)
799          }
800  
801          // Create area for Mac ROM
802 <        ROMBaseHost = (uint8 *)(ROM_BASE + NATMEM_OFFSET);
835 <        if (vm_acquire_fixed(ROMBaseHost, ROM_AREA_SIZE) < 0) {
802 >        if (vm_mac_acquire(ROM_BASE, ROM_AREA_SIZE) < 0) {
803                  sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
804                  ErrorAlert(str);
805                  goto quit;
806          }
807 +        ROMBaseHost = Mac2HostAddr(ROM_BASE);
808   #if !EMULATED_PPC
809          if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
810                  sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
# Line 853 | Line 821 | int main(int argc, char **argv)
821                  WarningAlert(GetString(STR_SMALL_RAM_WARN));
822                  RAMSize = 8*1024*1024;
823          }
824 <
825 <        RAMBaseHost = (uint8 *)(RAM_BASE + NATMEM_OFFSET);
826 <        if (vm_acquire_fixed(RAMBaseHost, RAMSize) < 0) {
827 <                sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
828 <                ErrorAlert(str);
829 <                goto quit;
824 >        memory_mapped_from_zero = false;
825 > #if REAL_ADDRESSING && HAVE_LINKER_SCRIPT
826 >        if (vm_mac_acquire(0, RAMSize) == 0) {
827 >                D(bug("Could allocate RAM from 0x0000\n"));
828 >                RAMBase = 0;
829 >                memory_mapped_from_zero = true;
830          }
831 + #endif
832 +        if (!memory_mapped_from_zero) {
833 + #ifndef PAGEZERO_HACK
834 +                // Create Low Memory area (0x0000..0x3000)
835 +                if (vm_mac_acquire(0, 0x3000) < 0) {
836 +                        sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
837 +                        ErrorAlert(str);
838 +                        goto quit;
839 +                }
840 +                lm_area_mapped = true;
841 + #endif
842 +                if (vm_mac_acquire(RAM_BASE, RAMSize) < 0) {
843 +                        sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
844 +                        ErrorAlert(str);
845 +                        goto quit;
846 +                }
847 +                RAMBase = RAM_BASE;
848 +        }
849 +        RAMBaseHost = Mac2HostAddr(RAMBase);
850   #if !EMULATED_PPC
851          if (vm_protect(RAMBaseHost, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
852                  sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
# Line 867 | Line 854 | int main(int argc, char **argv)
854                  goto quit;
855          }
856   #endif
870        RAMBase = RAM_BASE;
857          ram_area_mapped = true;
858          D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase));
859  
# Line 905 | Line 891 | int main(int argc, char **argv)
891          }
892          delete[] rom_tmp;
893  
894 <        // Load NVRAM
895 <        XPRAMInit();
910 <
911 <        // Load XPRAM default values if signature not found
912 <        if (XPRAM[0x130c] != 0x4e || XPRAM[0x130d] != 0x75
913 <         || XPRAM[0x130e] != 0x4d || XPRAM[0x130f] != 0x63) {
914 <                D(bug("Loading XPRAM default values\n"));
915 <                memset(XPRAM + 0x1300, 0, 0x100);
916 <                XPRAM[0x130c] = 0x4e;   // "NuMc" signature
917 <                XPRAM[0x130d] = 0x75;
918 <                XPRAM[0x130e] = 0x4d;
919 <                XPRAM[0x130f] = 0x63;
920 <                XPRAM[0x1301] = 0x80;   // InternalWaitFlags = DynWait (don't wait for SCSI devices upon bootup)
921 <                XPRAM[0x1310] = 0xa8;   // Standard PRAM values
922 <                XPRAM[0x1311] = 0x00;
923 <                XPRAM[0x1312] = 0x00;
924 <                XPRAM[0x1313] = 0x22;
925 <                XPRAM[0x1314] = 0xcc;
926 <                XPRAM[0x1315] = 0x0a;
927 <                XPRAM[0x1316] = 0xcc;
928 <                XPRAM[0x1317] = 0x0a;
929 <                XPRAM[0x131c] = 0x00;
930 <                XPRAM[0x131d] = 0x02;
931 <                XPRAM[0x131e] = 0x63;
932 <                XPRAM[0x131f] = 0x00;
933 <                XPRAM[0x1308] = 0x13;
934 <                XPRAM[0x1309] = 0x88;
935 <                XPRAM[0x130a] = 0x00;
936 <                XPRAM[0x130b] = 0xcc;
937 <                XPRAM[0x1376] = 0x00;   // OSDefault = MacOS
938 <                XPRAM[0x1377] = 0x01;
939 <        }
940 <
941 <        // Set boot volume
942 <        i16 = PrefsFindInt32("bootdrive");
943 <        XPRAM[0x1378] = i16 >> 8;
944 <        XPRAM[0x1379] = i16 & 0xff;
945 <        i16 = PrefsFindInt32("bootdriver");
946 <        XPRAM[0x137a] = i16 >> 8;
947 <        XPRAM[0x137b] = i16 & 0xff;
948 <
949 <        // Create BootGlobs at top of Mac memory
950 <        memset(RAMBaseHost + RAMSize - 4096, 0, 4096);
951 <        BootGlobsAddr = RAMBase + RAMSize - 0x1c;
952 <        WriteMacInt32(BootGlobsAddr - 5 * 4, RAMBase + RAMSize);        // MemTop
953 <        WriteMacInt32(BootGlobsAddr + 0 * 4, RAMBase);                          // First RAM bank
954 <        WriteMacInt32(BootGlobsAddr + 1 * 4, RAMSize);
955 <        WriteMacInt32(BootGlobsAddr + 2 * 4, (uint32)-1);                       // End of bank table
956 <
957 <        // Init thunks
958 <        if (!ThunksInit())
959 <                goto quit;
960 <
961 <        // Init drivers
962 <        SonyInit();
963 <        DiskInit();
964 <        CDROMInit();
965 <        SCSIInit();
966 <
967 <        // Init external file system
968 <        ExtFSInit();
969 <
970 <        // Init ADB
971 <        ADBInit();
972 <
973 <        // Init audio
974 <        AudioInit();
975 <
976 <        // Init network
977 <        EtherInit();
978 <
979 <        // Init serial ports
980 <        SerialInit();
981 <
982 <        // Init Time Manager
983 <        TimerInit();
984 <
985 <        // Init clipboard
986 <        ClipInit();
987 <
988 <        // Init video
989 <        if (!VideoInit())
990 <                goto quit;
991 <
992 <        // Install ROM patches
993 <        if (!PatchROM()) {
994 <                ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR));
894 >        // Initialize everything
895 >        if (!InitAll())
896                  goto quit;
897 <        }
897 >        D(bug("Initialization complete\n"));
898  
899          // Clear caches (as we loaded and patched code) and write protect ROM
900   #if !EMULATED_PPC
901 <        MakeExecutable(0, ROM_BASE, ROM_AREA_SIZE);
901 >        flush_icache_range(ROM_BASE, ROM_BASE + ROM_AREA_SIZE);
902   #endif
903          vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE);
904  
1004        // Initialize Kernel Data
1005        memset(kernel_data, 0, sizeof(KernelData));
1006        if (ROMType == ROMTYPE_NEWWORLD) {
1007                uint32 of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32));
1008                Mac_memset(of_dev_tree, 0, 4 * sizeof(uint32));
1009                uint32 vector_lookup_tbl = SheepMem::Reserve(128);
1010                uint32 vector_mask_tbl = SheepMem::Reserve(64);
1011                memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80);
1012                Mac_memset(vector_lookup_tbl, 0, 128);
1013                Mac_memset(vector_mask_tbl, 0, 64);
1014                kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE);
1015                kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree);                        // OF device tree base
1016                kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl);
1017                kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl);
1018                kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE);                           // OpenPIC base
1019                kernel_data->v[0xbb0 >> 2] = htonl(0);                                          // ADB base
1020                kernel_data->v[0xc20 >> 2] = htonl(RAMSize);
1021                kernel_data->v[0xc24 >> 2] = htonl(RAMSize);
1022                kernel_data->v[0xc30 >> 2] = htonl(RAMSize);
1023                kernel_data->v[0xc34 >> 2] = htonl(RAMSize);
1024                kernel_data->v[0xc38 >> 2] = htonl(0x00010020);
1025                kernel_data->v[0xc3c >> 2] = htonl(0x00200001);
1026                kernel_data->v[0xc40 >> 2] = htonl(0x00010000);
1027                kernel_data->v[0xc50 >> 2] = htonl(RAMBase);
1028                kernel_data->v[0xc54 >> 2] = htonl(RAMSize);
1029                kernel_data->v[0xf60 >> 2] = htonl(PVR);
1030                kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed);                      // clock-frequency
1031                kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed);                      // bus-frequency
1032                kernel_data->v[0xf6c >> 2] = htonl(TimebaseSpeed);                      // timebase-frequency
1033        } else {
1034                kernel_data->v[0xc80 >> 2] = htonl(RAMSize);
1035                kernel_data->v[0xc84 >> 2] = htonl(RAMSize);
1036                kernel_data->v[0xc90 >> 2] = htonl(RAMSize);
1037                kernel_data->v[0xc94 >> 2] = htonl(RAMSize);
1038                kernel_data->v[0xc98 >> 2] = htonl(0x00010020);
1039                kernel_data->v[0xc9c >> 2] = htonl(0x00200001);
1040                kernel_data->v[0xca0 >> 2] = htonl(0x00010000);
1041                kernel_data->v[0xcb0 >> 2] = htonl(RAMBase);
1042                kernel_data->v[0xcb4 >> 2] = htonl(RAMSize);
1043                kernel_data->v[0xf80 >> 2] = htonl(PVR);
1044                kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed);                      // clock-frequency
1045                kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed);                      // bus-frequency
1046                kernel_data->v[0xf8c >> 2] = htonl(TimebaseSpeed);                      // timebase-frequency
1047        }
1048
1049        // Initialize extra low memory
1050        D(bug("Initializing Low Memory...\n"));
1051        Mac_memset(0, 0, 0x3000);
1052        WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h'));                  // Signature to detect SheepShaver
1053        WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr);                                 // For trap replacement routines
1054        WriteMacInt32(XLM_PVR, PVR);                                                                    // Theoretical PVR
1055        WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed);                                    // For DriverServicesLib patch
1056        WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN);                // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode)
1057        WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage());                             // Pointer to read-only page with all bits set to 0
1058 #if !EMULATED_PPC
1059        WriteMacInt32(XLM_TOC, (uint32)TOC);                                                            // TOC pointer of emulator
1060 #endif
1061        WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT));       // DLPI ethernet driver functions
1062        WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM));
1063        WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN));
1064        WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE));
1065        WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT));
1066        WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV));
1067        WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO));
1068        D(bug("Low Memory initialized\n"));
1069
905          // Start 60Hz thread
906          tick_thread_cancel = false;
907          tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0);
# Line 1088 | Line 923 | int main(int argc, char **argv)
923          sigill_action.sa_restorer = NULL;
924   #endif
925          if (sigaction(SIGILL, &sigill_action, NULL) < 0) {
926 <                sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno));
926 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno));
927                  ErrorAlert(str);
928                  goto quit;
929          }
# Line 1097 | Line 932 | int main(int argc, char **argv)
932   #if !EMULATED_PPC
933          // Install interrupt signal handler
934          sigemptyset(&sigusr2_action.sa_mask);
935 <        sigusr2_action.sa_sigaction = sigusr2_handler;
935 >        sigusr2_action.sa_sigaction = sigusr2_handler_init;
936          sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO;
937   #ifdef HAVE_SIGNAL_SA_RESTORER
938          sigusr2_action.sa_restorer = NULL;
939   #endif
940          if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) {
941 <                sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno));
941 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGUSR2", strerror(errno));
942                  ErrorAlert(str);
943                  goto quit;
944          }
# Line 1160 | Line 995 | static void Quit(void)
995          sigaction(SIGILL, &sigill_action, NULL);
996  
997          // Delete stacks for signal handlers
998 <        for (int i = 0; i < SIG_STACK_COUNT; i++) {
999 <                void *sig_stack = sig_stacks[i].ss_sp;
1000 <                if (sig_stack)
1001 <                        free(sig_stack);
1167 <        }
998 >        if (sig_stack.ss_sp)
999 >                free(sig_stack.ss_sp);
1000 >        if (extra_stack.ss_sp)
1001 >                free(extra_stack.ss_sp);
1002   #endif
1003  
1004 <        // Save NVRAM
1005 <        XPRAMExit();
1172 <
1173 <        // Exit clipboard
1174 <        ClipExit();
1175 <
1176 <        // Exit Time Manager
1177 <        TimerExit();
1178 <
1179 <        // Exit serial
1180 <        SerialExit();
1181 <
1182 <        // Exit network
1183 <        EtherExit();
1184 <
1185 <        // Exit audio
1186 <        AudioExit();
1187 <
1188 <        // Exit ADB
1189 <        ADBExit();
1190 <
1191 <        // Exit video
1192 <        VideoExit();
1193 <
1194 <        // Exit external file system
1195 <        ExtFSExit();
1196 <
1197 <        // Exit drivers
1198 <        SCSIExit();
1199 <        CDROMExit();
1200 <        DiskExit();
1201 <        SonyExit();
1202 <
1203 <        // Delete thunks
1204 <        ThunksExit();
1004 >        // Deinitialize everything
1005 >        ExitAll();
1006  
1007          // Delete SheepShaver globals
1008          SheepMem::Exit();
1009  
1010          // Delete RAM area
1011          if (ram_area_mapped)
1012 <                vm_release(RAMBaseHost, RAMSize);
1012 >                vm_mac_release(RAMBase, RAMSize);
1013  
1014          // Delete ROM area
1015          if (rom_area_mapped)
1016 <                vm_release(ROMBaseHost, ROM_AREA_SIZE);
1016 >                vm_mac_release(ROM_BASE, ROM_AREA_SIZE);
1017  
1018          // Delete DR cache areas
1019          if (dr_emulator_area_mapped)
1020 <                vm_release((void *)(DR_EMULATOR_BASE + NATMEM_OFFSET), DR_EMULATOR_SIZE);
1020 >                vm_mac_release(DR_EMULATOR_BASE, DR_EMULATOR_SIZE);
1021          if (dr_cache_area_mapped)
1022 <                vm_release((void *)(DR_CACHE_BASE + NATMEM_OFFSET), DR_CACHE_SIZE);
1022 >                vm_mac_release(DR_CACHE_BASE, DR_CACHE_SIZE);
1023  
1024          // Delete Kernel Data area
1025 <        if (kernel_area >= 0) {
1225 <                shmdt((void *)(KERNEL_DATA_BASE + NATMEM_OFFSET));
1226 <                shmdt((void *)(KERNEL_DATA2_BASE + NATMEM_OFFSET));
1227 <                shmctl(kernel_area, IPC_RMID, NULL);
1228 <        }
1025 >        kernel_data_exit();
1026  
1027          // Delete Low Memory area
1028          if (lm_area_mapped)
1029 <                vm_release((void *)NATMEM_OFFSET, 0x3000);
1029 >                vm_mac_release(0, 0x3000);
1030  
1031          // Close /dev/zero
1032          if (zero_fd > 0)
# Line 1252 | Line 1049 | static void Quit(void)
1049                  XCloseDisplay(x_display);
1050   #endif
1051  
1052 +        // Notify GUI we are about to leave
1053 +        if (gui_connection) {
1054 +                if (rpc_method_invoke(gui_connection, RPC_METHOD_EXIT, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR)
1055 +                        rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID);
1056 +        }
1057 +
1058          exit(0);
1059   }
1060  
1061  
1062   /*
1063 + *  Initialize Kernel Data segments
1064 + */
1065 +
1066 + static bool kernel_data_init(void)
1067 + {
1068 +        char str[256];
1069 +        uint32 kernel_area_size = (KERNEL_AREA_SIZE + SHMLBA - 1) & -SHMLBA;
1070 +
1071 +        kernel_area = shmget(IPC_PRIVATE, kernel_area_size, 0600);
1072 +        if (kernel_area == -1) {
1073 +                sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno));
1074 +                ErrorAlert(str);
1075 +                return false;
1076 +        }
1077 +        void *kernel_addr = Mac2HostAddr(KERNEL_DATA_BASE & -SHMLBA);
1078 +        if (shmat(kernel_area, kernel_addr, 0) != kernel_addr) {
1079 +                sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno));
1080 +                ErrorAlert(str);
1081 +                return false;
1082 +        }
1083 +        kernel_addr = Mac2HostAddr(KERNEL_DATA2_BASE & -SHMLBA);
1084 +        if (shmat(kernel_area, kernel_addr, 0) != kernel_addr) {
1085 +                sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno));
1086 +                ErrorAlert(str);
1087 +                return false;
1088 +        }
1089 +        return true;
1090 + }
1091 +
1092 +
1093 + /*
1094 + *  Deallocate Kernel Data segments
1095 + */
1096 +
1097 + static void kernel_data_exit(void)
1098 + {
1099 +        if (kernel_area >= 0) {
1100 +                shmdt(Mac2HostAddr(KERNEL_DATA_BASE & -SHMLBA));
1101 +                shmdt(Mac2HostAddr(KERNEL_DATA2_BASE & -SHMLBA));
1102 +                shmctl(kernel_area, IPC_RMID, NULL);
1103 +        }
1104 + }
1105 +
1106 +
1107 + /*
1108   *  Jump into Mac ROM, start 680x0 emulator
1109   */
1110  
# Line 1343 | Line 1191 | void QuitEmulator(void)
1191  
1192  
1193   /*
1346 *  Pause/resume emulator
1347 */
1348
1349 void PauseEmulator(void)
1350 {
1351        pthread_kill(emul_thread, SIGSTOP);
1352 }
1353
1354 void ResumeEmulator(void)
1355 {
1356        pthread_kill(emul_thread, SIGCONT);
1357 }
1358
1359
1360 /*
1194   *  Dump 68k registers
1195   */
1196  
# Line 1392 | Line 1225 | void MakeExecutable(int dummy, uint32 st
1225   #if EMULATED_PPC
1226          FlushCodeCache(start, start + length);
1227   #else
1228 <        flush_icache_range(start, (void *)(start + length));
1228 >        flush_icache_range(start, start + length);
1229   #endif
1230   }
1231  
1232  
1233   /*
1401 *  Patch things after system startup (gets called by disk driver accRun routine)
1402 */
1403
1404 void PatchAfterStartup(void)
1405 {
1406        ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL);
1407        InstallExtFS();
1408 }
1409
1410
1411 /*
1234   *  NVRAM watchdog thread (saves NVRAM every minute)
1235   */
1236  
# Line 1511 | Line 1333 | static void *tick_func(void *arg)
1333          }
1334  
1335          uint64 end = GetTicks_usec();
1336 <        D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1336 >        D(bug("%lld ticks in %lld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1337          return NULL;
1338   }
1339  
# Line 1561 | Line 1383 | struct B2_mutex {
1383              pthread_mutexattr_init(&attr);
1384              // Initialize the mutex for priority inheritance --
1385              // required for accurate timing.
1386 < #ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL
1386 > #if defined(HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL) && !defined(__CYGWIN__)
1387              pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
1388   #endif
1389   #if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL)
# Line 1635 | Line 1457 | void B2_delete_mutex(B2_mutex *mutex)
1457   #if !EMULATED_PPC
1458   void TriggerInterrupt(void)
1459   {
1460 <        if (ready_for_signals)
1460 >        if (ready_for_signals) {
1461 >                idle_resume();
1462                  pthread_kill(emul_thread, SIGUSR2);
1463 +        }
1464   }
1465   #endif
1466  
# Line 1691 | Line 1515 | void EnableInterrupt(void)
1515   */
1516  
1517   #if !EMULATED_PPC
1518 < static void sigusr2_handler(int sig, siginfo_t *sip, void *scp)
1518 > void sigusr2_handler(int sig, siginfo_t *sip, void *scp)
1519   {
1520          machine_regs *r = MACHINE_REGISTERS(scp);
1521  
1522 + #ifdef SYSTEM_CLOBBERS_R2
1523 +        // Restore pointer to Thread Local Storage
1524 +        set_r2(TOC);
1525 + #endif
1526 + #ifdef SYSTEM_CLOBBERS_R13
1527 +        // Restore pointer to .sdata section
1528 +        set_r13(R13);
1529 + #endif
1530 +
1531   #ifdef USE_SDL_VIDEO
1532          // We must fill in the events queue in the same thread that did call SDL_SetVideoMode()
1533          SDL_PumpEvents();
# Line 1720 | Line 1553 | static void sigusr2_handler(int sig, sig
1553                          // 68k emulator inactive, in nanokernel?
1554                          if (r->gpr(1) != KernelDataAddr) {
1555  
1556 <                                // Set extra stack for nested interrupts
1557 <                                sig_stack_acquire();
1556 >                                // Set extra stack for SIGSEGV handler
1557 >                                sigaltstack(&extra_stack, NULL);
1558                                  
1559                                  // Prepare for 68k interrupt level 1
1560                                  WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1);
# Line 1734 | Line 1567 | static void sigusr2_handler(int sig, sig
1567                                  else
1568                                          ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr);
1569  
1570 <                                // Reset normal signal stack
1571 <                                sig_stack_release();
1570 >                                // Reset normal stack
1571 >                                sigaltstack(&sig_stack, NULL);
1572                          }
1573                          break;
1574   #endif
# Line 1746 | Line 1579 | static void sigusr2_handler(int sig, sig
1579                          if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
1580  
1581                                  // Set extra stack for SIGSEGV handler
1582 <                                sig_stack_acquire();
1582 >                                sigaltstack(&extra_stack, NULL);
1583   #if 1
1584                                  // Execute full 68k interrupt routine
1585                                  M68kRegisters r;
# Line 1772 | Line 1605 | static void sigusr2_handler(int sig, sig
1605                                          }
1606                                  }
1607   #endif
1608 <                                // Reset normal signal stack
1609 <                                sig_stack_release();
1608 >                                // Reset normal stack
1609 >                                sigaltstack(&sig_stack, NULL);
1610                          }
1611                          break;
1612   #endif
# Line 1794 | Line 1627 | static void sigsegv_handler(int sig, sig
1627          // Get effective address
1628          uint32 addr = r->dar();
1629          
1630 + #ifdef SYSTEM_CLOBBERS_R2
1631 +        // Restore pointer to Thread Local Storage
1632 +        set_r2(TOC);
1633 + #endif
1634 + #ifdef SYSTEM_CLOBBERS_R13
1635 +        // Restore pointer to .sdata section
1636 +        set_r13(R13);
1637 + #endif
1638 +
1639   #if ENABLE_VOSF
1640          // Handle screen fault.
1641          extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction);
# Line 2043 | Line 1885 | static void sigill_handler(int sig, sigi
1885          machine_regs *r = MACHINE_REGISTERS(scp);
1886          char str[256];
1887  
1888 + #ifdef SYSTEM_CLOBBERS_R2
1889 +        // Restore pointer to Thread Local Storage
1890 +        set_r2(TOC);
1891 + #endif
1892 + #ifdef SYSTEM_CLOBBERS_R13
1893 +        // Restore pointer to .sdata section
1894 +        set_r13(R13);
1895 + #endif
1896 +
1897          // Fault in Mac ROM or RAM?
1898          bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize));
1899          if (mac_fault) {
# Line 2211 | Line 2062 | bool SheepMem::Init(void)
2062          page_size = getpagesize();
2063  
2064          // Allocate SheepShaver globals
2065 <        if (vm_acquire_fixed((char *)(base + NATMEM_OFFSET), size) < 0)
2065 >        proc = base;
2066 >        if (vm_mac_acquire(base, size) < 0)
2067                  return false;
2068  
2069 <        // Allocate page with all bits set to 0
2070 <        zero_page = base + size;
2071 <        uint8 *zero_page_host = (uint8 *)zero_page + NATMEM_OFFSET;
2072 <        if (vm_acquire_fixed(zero_page_host, page_size) < 0)
2073 <                return false;
2222 <        memset(zero_page_host, 0, page_size);
2223 <        if (vm_protect(zero_page_host, page_size, VM_PAGE_READ) < 0)
2069 >        // Allocate page with all bits set to 0, right in the middle
2070 >        // This is also used to catch undesired overlaps between proc and data areas
2071 >        zero_page = proc + (size / 2);
2072 >        Mac_memset(zero_page, 0, page_size);
2073 >        if (vm_protect(Mac2HostAddr(zero_page), page_size, VM_PAGE_READ) < 0)
2074                  return false;
2075  
2076   #if EMULATED_PPC
2077          // Allocate alternate stack for PowerPC interrupt routine
2078 <        sig_stack = zero_page + page_size;
2079 <        if (vm_acquire_fixed((char *)(sig_stack + NATMEM_OFFSET), SIG_STACK_SIZE) < 0)
2078 >        sig_stack = base + size;
2079 >        if (vm_mac_acquire(sig_stack, SIG_STACK_SIZE) < 0)
2080                  return false;
2081   #endif
2082  
2083 <        top = base + size;
2083 >        data = base + size;
2084          return true;
2085   }
2086  
2087   void SheepMem::Exit(void)
2088   {
2089 <        if (top) {
2089 >        if (data) {
2090                  // Delete SheepShaver globals
2091 <                vm_release((void *)(base + NATMEM_OFFSET), size);
2242 <
2243 <                // Delete zero page
2244 <                vm_release((void *)(zero_page + NATMEM_OFFSET), page_size);
2091 >                vm_mac_release(base, size);
2092  
2093   #if EMULATED_PPC
2094                  // Delete alternate stack for PowerPC interrupt routine
2095 <                vm_release((void *)(sig_stack + NATMEM_OFFSET), SIG_STACK_SIZE);
2095 >                vm_mac_release(sig_stack, SIG_STACK_SIZE);
2096   #endif
2097          }
2098   }
# Line 2300 | Line 2147 | void display_alert(int title_id, int pre
2147  
2148   void ErrorAlert(const char *text)
2149   {
2150 +        if (gui_connection) {
2151 +                if (rpc_method_invoke(gui_connection, RPC_METHOD_ERROR_ALERT, RPC_TYPE_STRING, text, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR &&
2152 +                        rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR)
2153 +                        return;
2154 +        }
2155   #if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO)
2156          if (PrefsFindBool("nogui") || x_display == NULL) {
2157                  printf(GetString(STR_SHELL_ERROR_PREFIX), text);
# Line 2319 | Line 2171 | void ErrorAlert(const char *text)
2171  
2172   void WarningAlert(const char *text)
2173   {
2174 +        if (gui_connection) {
2175 +                if (rpc_method_invoke(gui_connection, RPC_METHOD_WARNING_ALERT, RPC_TYPE_STRING, text, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR &&
2176 +                        rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR)
2177 +                        return;
2178 +        }
2179   #if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO)
2180          if (PrefsFindBool("nogui") || x_display == NULL) {
2181                  printf(GetString(STR_SHELL_WARNING_PREFIX), text);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines