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.90 by asvitkine, 2010-08-22T18:57:11Z

# 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) 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 92 | Line 89
89   #include <sys/mman.h>
90   #include <sys/ipc.h>
91   #include <sys/shm.h>
92 + #include <sys/stat.h>
93   #include <signal.h>
94  
95   #include "sysdeps.h"
# Line 105 | Line 103
103   #include "xpram.h"
104   #include "timer.h"
105   #include "adb.h"
108 #include "sony.h"
109 #include "disk.h"
110 #include "cdrom.h"
111 #include "scsi.h"
106   #include "video.h"
113 #include "audio.h"
114 #include "ether.h"
115 #include "serial.h"
116 #include "clip.h"
117 #include "extfs.h"
107   #include "sys.h"
108   #include "macos_util.h"
109   #include "rom_patches.h"
110   #include "user_strings.h"
111   #include "vm_alloc.h"
112   #include "sigsegv.h"
113 < #include "thunks.h"
113 > #include "sigregs.h"
114 > #include "rpc.h"
115  
116   #define DEBUG 0
117   #include "debug.h"
# Line 146 | Line 136
136   #ifdef ENABLE_XF86_DGA
137   #include <X11/Xlib.h>
138   #include <X11/Xutil.h>
139 < #include <X11/extensions/xf86dga.h>
139 > #include <X11/extensions/Xxf86dga.h>
140   #endif
141  
142   #ifdef ENABLE_MON
# Line 166 | Line 156
156   // Interrupts in native mode?
157   #define INTERRUPTS_IN_NATIVE_MODE 1
158  
169 // Number of alternate stacks for signal handlers?
170 #define SIG_STACK_COUNT 4
171
159  
160   // Constants
161   const char ROM_FILE_NAME[] = "ROM";
162   const char ROM_FILE_NAME2[] = "Mac OS ROM";
163  
164 < #if REAL_ADDRESSING
178 < const uintptr RAM_BASE = 0x20000000;            // Base address of RAM
179 < #else
164 > #if !REAL_ADDRESSING
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 <
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 < }
168 > const uintptr ROM_BASE = 0x40800000;            // Base address of ROM
169 > #if REAL_ADDRESSING
170 > const uint32 ROM_ALIGNMENT = 0x100000;          // ROM must be aligned to a 1MB boundary
171   #endif
172 + const uint32 SIG_STACK_SIZE = 0x10000;          // Size of signal stack
173  
174  
175   // Global variables (exported)
176   #if !EMULATED_PPC
177 < void *TOC;                              // Small data pointer (r13)
177 > void *TOC = NULL;               // Pointer to Thread Local Storage (r2)
178 > void *R13 = NULL;               // Pointer to .sdata section (r13 under Linux)
179   #endif
180   uint32 RAMBase;                 // Base address of Mac RAM
181   uint32 RAMSize;                 // Size of Mac RAM
182 + uint32 ROMBase;                 // Base address of Mac ROM
183   uint32 KernelDataAddr;  // Address of Kernel Data
184   uint32 BootGlobsAddr;   // Address of BootGlobs structure at top of Mac RAM
185   uint32 DRCacheAddr;             // Address of DR Cache
# Line 327 | Line 229 | static uintptr sig_stack = 0;                          // Stac
229   #else
230   static struct sigaction sigsegv_action;         // Data access exception signal (of emulator thread)
231   static struct sigaction sigill_action;          // Illegal instruction signal (of emulator thread)
232 + static stack_t sig_stack;                                       // Stack for signal handlers
233 + static stack_t extra_stack;                                     // Stack for SIGSEGV inside interrupt handler
234   static bool emul_thread_fatal = false;          // Flag: MacOS thread crashed, tick thread shall dump debug output
235   static sigregs sigsegv_regs;                            // Register dump when crashed
236   static const char *crash_reason = NULL;         // Reason of the crash (SIGSEGV, SIGBUS, SIGILL)
237   #endif
238  
239 + static rpc_connection_t *gui_connection = NULL; // RPC connection to the GUI
240 + static const char *gui_connection_path = NULL;  // GUI connection identifier
241 +
242   uint32  SheepMem::page_size;                            // Size of a native page
243   uintptr SheepMem::zero_page = 0;                        // Address of ro page filled in with zeros
244   uintptr SheepMem::base = 0x60000000;            // Address of SheepShaver data
245 < uintptr SheepMem::top = 0;                                      // Top of SheepShaver data (stack like storage)
245 > uintptr SheepMem::proc;                                         // Bottom address of SheepShave procedures
246 > uintptr SheepMem::data;                                         // Top of SheepShaver data (stack like storage)
247  
248  
249   // Prototypes
250 + static bool kernel_data_init(void);
251 + static void kernel_data_exit(void);
252   static void Quit(void);
253   static void *emul_func(void *arg);
254   static void *nvram_func(void *arg);
# Line 347 | Line 257 | static void *tick_func(void *arg);
257   extern void emul_ppc(uint32 start);
258   extern void init_emul_ppc(void);
259   extern void exit_emul_ppc(void);
260 < sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
260 > sigsegv_return_t sigsegv_handler(sigsegv_info_t *sip);
261   #else
262 < static void sigusr2_handler(int sig, siginfo_t *sip, void *scp);
262 > extern "C" void sigusr2_handler_init(int sig, siginfo_t *sip, void *scp);
263 > extern "C" void sigusr2_handler(int sig, siginfo_t *sip, void *scp);
264   static void sigsegv_handler(int sig, siginfo_t *sip, void *scp);
265   static void sigill_handler(int sig, siginfo_t *sip, void *scp);
266   #endif
# Line 357 | Line 268 | static void sigill_handler(int sig, sigi
268  
269   // From asm_linux.S
270   #if !EMULATED_PPC
360 extern "C" void *get_toc(void);
271   extern "C" void *get_sp(void);
272 < extern "C" void flush_icache_range(void *start, void *end);
272 > extern "C" void *get_r2(void);
273 > extern "C" void set_r2(void *);
274 > extern "C" void *get_r13(void);
275 > extern "C" void set_r13(void *);
276 > extern "C" void flush_icache_range(uint32 start, uint32 end);
277   extern "C" void jump_to_rom(uint32 entry, uint32 context);
278   extern "C" void quit_emulator(void);
279   extern "C" void execute_68k(uint32 pc, M68kRegisters *r);
# Line 423 | Line 337 | int atomic_or(int *var, int v)
337  
338  
339   /*
340 + *  Memory management helpers
341 + */
342 +
343 + static inline uint8 *vm_mac_acquire(uint32 size)
344 + {
345 +        return (uint8 *)vm_acquire(size);
346 + }
347 +
348 + static inline int vm_mac_acquire_fixed(uint32 addr, uint32 size)
349 + {
350 +        return vm_acquire_fixed(Mac2HostAddr(addr), size);
351 + }
352 +
353 + static inline int vm_mac_release(uint32 addr, uint32 size)
354 + {
355 +        return vm_release(Mac2HostAddr(addr), size);
356 + }
357 +
358 +
359 + /*
360   *  Main program
361   */
362  
# Line 435 | Line 369 | static void usage(const char *prg_name)
369          exit(0);
370   }
371  
372 + static bool valid_vmdir(const char *path)
373 + {
374 +        const int suffix_len = sizeof(".sheepvm") - 1;
375 +        int len = strlen(path);
376 +        if (len && path[len - 1] == '/') // to support both ".sheepvm" and ".sheepvm/"
377 +                len--;
378 +        if (len > suffix_len && !strncmp(path + len - suffix_len, ".sheepvm", suffix_len)) {
379 +                struct stat d;
380 +                if (!stat(path, &d) && S_ISDIR(d.st_mode)) {
381 +                        return true;
382 +                }
383 +        }
384 +        return false;
385 + }
386 +
387   int main(int argc, char **argv)
388   {
389          char str[256];
441        int16 i16;
390          int rom_fd;
391          FILE *proc_file;
392          const char *rom_path;
393          uint32 rom_size, actual;
394          uint8 *rom_tmp;
395          time_t now, expire;
396 +        bool memory_mapped_from_zero, ram_rom_areas_contiguous;
397 +        const char *vmdir = NULL;
398 +
399 + #ifdef USE_SDL_VIDEO
400 +        // Don't let SDL block the screensaver
401 +        setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", TRUE);
402 +
403 +        // Make SDL pass through command-clicks and option-clicks unaltered
404 +        setenv("SDL_HAS3BUTTONMOUSE", "1", TRUE);
405 + #endif
406  
407          // Initialize variables
408          RAMBase = 0;
# Line 455 | Line 413 | int main(int argc, char **argv)
413          printf(" %s\n", GetString(STR_ABOUT_TEXT2));
414  
415   #if !EMULATED_PPC
416 + #ifdef SYSTEM_CLOBBERS_R2
417          // Get TOC pointer
418 <        TOC = get_toc();
418 >        TOC = get_r2();
419 > #endif
420 > #ifdef SYSTEM_CLOBBERS_R13
421 >        // Get r13 register
422 >        R13 = get_r13();
423   #endif
461
462 #ifdef ENABLE_GTK
463        // Init GTK
464        gtk_set_locale();
465        gtk_init(&argc, &argv);
424   #endif
467
468        // Read preferences
469        PrefsInit(argc, argv);
425  
426          // Parse command line arguments
427          for (int i=1; i<argc; i++) {
# Line 478 | Line 433 | int main(int argc, char **argv)
433                          if (i < argc)
434                                  x_display_name = strdup(argv[i]);
435   #endif
436 <                } else if (argv[i][0] == '-') {
436 >                } else if (strcmp(argv[i], "--gui-connection") == 0) {
437 >                        argv[i++] = NULL;
438 >                        if (i < argc) {
439 >                                gui_connection_path = argv[i];
440 >                                argv[i] = NULL;
441 >                        }
442 >                } else if (valid_vmdir(argv[i])) {
443 >                        vmdir = argv[i];
444 >                        argv[i] = NULL;
445 >                        printf("Using %s as vmdir.\n", vmdir);
446 >                        if (chdir(vmdir)) {
447 >                                printf("Failed to chdir to %s. Good bye.", vmdir);
448 >                                exit(1);
449 >                        }
450 >                        break;
451 >                }
452 >        }
453 >
454 >        // Remove processed arguments
455 >        for (int i=1; i<argc; i++) {
456 >                int k;
457 >                for (k=i; k<argc; k++)
458 >                        if (argv[k] != NULL)
459 >                                break;
460 >                if (k > i) {
461 >                        k -= i;
462 >                        for (int j=i+k; j<argc; j++)
463 >                                argv[j-k] = argv[j];
464 >                        argc -= k;
465 >                }
466 >        }
467 >
468 >        // Connect to the external GUI
469 >        if (gui_connection_path) {
470 >                if ((gui_connection = rpc_init_client(gui_connection_path)) == NULL) {
471 >                        fprintf(stderr, "Failed to initialize RPC client connection to the GUI\n");
472 >                        return 1;
473 >                }
474 >        }
475 >
476 > #ifdef ENABLE_GTK
477 >        if (!gui_connection) {
478 >                // Init GTK
479 >                gtk_set_locale();
480 >                gtk_init(&argc, &argv);
481 >        }
482 > #endif
483 >
484 >        // Read preferences
485 >        PrefsInit(vmdir, argc, argv);
486 >
487 >        // Any command line arguments left?
488 >        for (int i=1; i<argc; i++) {
489 >                if (argv[i][0] == '-') {
490                          fprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
491                          usage(argv[0]);
492                  }
# Line 501 | Line 509 | int main(int argc, char **argv)
509                  goto quit;
510          }
511          atexit(SDL_Quit);
512 +
513 +        // Don't let SDL catch SIGINT and SIGTERM signals
514 +        signal(SIGINT, SIG_DFL);
515 +        signal(SIGTERM, SIG_DFL);
516   #endif
517  
518   #ifndef USE_SDL_VIDEO
# Line 526 | Line 538 | int main(int argc, char **argv)
538  
539   #if !EMULATED_PPC
540          // Create and install stacks for signal handlers
541 <        for (int i = 0; i < SIG_STACK_COUNT; i++) {
542 <                void *sig_stack = malloc(SIG_STACK_SIZE);
543 <                D(bug("Signal stack %d at %p\n", i, sig_stack));
544 <                if (sig_stack == NULL) {
545 <                        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;
541 >        sig_stack.ss_sp = malloc(SIG_STACK_SIZE);
542 >        D(bug("Signal stack at %p\n", sig_stack.ss_sp));
543 >        if (sig_stack.ss_sp == NULL) {
544 >                ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
545 >                goto quit;
546          }
547 <        sig_stack_id = 0;
548 <        if (sigaltstack(&sig_stacks[0], NULL) < 0) {
547 >        sig_stack.ss_flags = 0;
548 >        sig_stack.ss_size = SIG_STACK_SIZE;
549 >        if (sigaltstack(&sig_stack, NULL) < 0) {
550                  sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
551                  ErrorAlert(str);
552                  goto quit;
553          }
554 +        extra_stack.ss_sp = malloc(SIG_STACK_SIZE);
555 +        D(bug("Extra stack at %p\n", extra_stack.ss_sp));
556 +        if (extra_stack.ss_sp == NULL) {
557 +                ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
558 +                goto quit;
559 +        }
560 +        extra_stack.ss_flags = 0;
561 +        extra_stack.ss_size = SIG_STACK_SIZE;
562   #endif
563  
564   #if !EMULATED_PPC
# Line 555 | Line 571 | int main(int argc, char **argv)
571          sigsegv_action.sa_restorer = NULL;
572   #endif
573          if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) {
574 <                sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno));
574 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGSEGV", strerror(errno));
575                  ErrorAlert(str);
576                  goto quit;
577          }
578          if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) {
579 <                sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno));
579 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGBUS", strerror(errno));
580                  ErrorAlert(str);
581                  goto quit;
582          }
583   #else
584          // Install SIGSEGV handler for CPU emulator
585          if (!sigsegv_install_handler(sigsegv_handler)) {
586 <                sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno));
586 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGSEGV", strerror(errno));
587                  ErrorAlert(str);
588                  goto quit;
589          }
# Line 659 | Line 675 | int main(int argc, char **argv)
675                          { 0xffff0000, 0x80010000, "7455" },
676                          { 0xffff0000, 0x80020000, "7457" },
677                          { 0xffff0000, 0x80030000, "7447A" },
678 +                        { 0xffff0000, 0x80040000, "7448" },
679                          { 0x7fff0000, 0x00810000, "82xx" },
680                          { 0x7fff0000, 0x00820000, "8280" },
681                          { 0xffff0000, 0x00400000, "Power3 (630)" },
# Line 667 | Line 684 | int main(int argc, char **argv)
684                          { 0xffff0000, 0x00370000, "S-star" },
685                          { 0xffff0000, 0x00350000, "Power4" },
686                          { 0xffff0000, 0x00390000, "PPC970" },
687 +                        { 0xffff0000, 0x003c0000, "PPC970FX" },
688 +                        { 0xffff0000, 0x003a0000, "POWER5 (gr)" },
689 +                        { 0xffff0000, 0x003b0000, "POWER5+ (gs)" },
690 +                        { 0xffff0000, 0x003e0000, "POWER6" },
691 +                        { 0xffff0000, 0x00700000, "Cell Broadband Engine" },
692 +                        { 0x7fff0000, 0x00900000, "PA6T" },
693                          { 0, 0, 0 }
694                  };
695  
# Line 680 | Line 703 | int main(int argc, char **argv)
703  
704                          // Parse line
705                          int i;
706 +                        float f;
707                          char value[256];
708 <                        if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) {
708 >                        if (sscanf(line, "cpu : %[^,]", value) == 1) {
709                                  // Search by name
710                                  const char *cpu_name = NULL;
711                                  for (int i = 0; cpu_specs[i].pvr_mask != 0; i++) {
# Line 696 | Line 720 | int main(int argc, char **argv)
720                                  else
721                                          printf("Found a PowerPC %s processor\n", cpu_name);
722                          }
723 <                        if (sscanf(line, "clock : %dMHz", &i) == 1)
723 >                        if (sscanf(line, "clock : %fMHz", &f) == 1)
724 >                                CPUClockSpeed = BusClockSpeed = ((int64)f) * 1000000;
725 >                        else if (sscanf(line, "clock : %dMHz", &i) == 1)
726                                  CPUClockSpeed = BusClockSpeed = i * 1000000;
727                  }
728                  fclose(proc_file);
# Line 740 | Line 766 | int main(int argc, char **argv)
766          case 0x8000:                            // 7450
767          case 0x8001:                            // 7455
768          case 0x8002:                            // 7457
769 +        case 0x8003:                            // 7447A
770 +        case 0x8004:                            // 7448
771          case 0x0039:                            //  970
772 +        case 0x003c:                            //  970FX
773                  PVR = 0x000c0000;               // 7400
774                  break;
775          }
# Line 767 | Line 796 | int main(int argc, char **argv)
796                  goto quit;
797          }
798  
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
799          // Create areas for Kernel Data
800 <        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);
800 >        if (!kernel_data_init())
801                  goto quit;
802 <        }
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);
795 <                goto quit;
796 <        }
797 <        kernel_data = (KernelData *)(KERNEL_DATA_BASE + NATMEM_OFFSET);
802 >        kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE);
803          emulator_data = &kernel_data->ed;
804          KernelDataAddr = KERNEL_DATA_BASE;
805          D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE));
806          D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed)));
807  
808          // Create area for DR Cache
809 <        if (vm_acquire_fixed((void *)(DR_EMULATOR_BASE + NATMEM_OFFSET), DR_EMULATOR_SIZE) < 0) {
809 >        if (vm_mac_acquire_fixed(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) {
810                  sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno));
811                  ErrorAlert(str);
812                  goto quit;
813          }
814          dr_emulator_area_mapped = true;
815 <        if (vm_acquire_fixed((void *)(DR_CACHE_BASE + NATMEM_OFFSET), DR_CACHE_SIZE) < 0) {
815 >        if (vm_mac_acquire_fixed(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) {
816                  sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno));
817                  ErrorAlert(str);
818                  goto quit;
# Line 829 | Line 834 | int main(int argc, char **argv)
834                  ErrorAlert(str);
835                  goto quit;
836          }
837 <
833 <        // Create area for Mac ROM
834 <        ROMBaseHost = (uint8 *)(ROM_BASE + NATMEM_OFFSET);
835 <        if (vm_acquire_fixed(ROMBaseHost, ROM_AREA_SIZE) < 0) {
836 <                sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
837 <                ErrorAlert(str);
838 <                goto quit;
839 <        }
840 < #if !EMULATED_PPC
841 <        if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
842 <                sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
843 <                ErrorAlert(str);
844 <                goto quit;
845 <        }
846 < #endif
847 <        rom_area_mapped = true;
848 <        D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROM_BASE));
849 <
837 >        
838          // Create area for Mac RAM
839          RAMSize = PrefsFindInt32("ramsize");
840          if (RAMSize < 8*1024*1024) {
841                  WarningAlert(GetString(STR_SMALL_RAM_WARN));
842                  RAMSize = 8*1024*1024;
843          }
844 <
845 <        RAMBaseHost = (uint8 *)(RAM_BASE + NATMEM_OFFSET);
846 <        if (vm_acquire_fixed(RAMBaseHost, RAMSize) < 0) {
847 <                sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
848 <                ErrorAlert(str);
849 <                goto quit;
844 >        memory_mapped_from_zero = false;
845 >        ram_rom_areas_contiguous = false;
846 > #if REAL_ADDRESSING && HAVE_LINKER_SCRIPT
847 >        if (vm_mac_acquire_fixed(0, RAMSize) == 0) {
848 >                D(bug("Could allocate RAM from 0x0000\n"));
849 >                RAMBase = 0;
850 >                RAMBaseHost = Mac2HostAddr(RAMBase);
851 >                memory_mapped_from_zero = true;
852 >        }
853 > #endif
854 >        if (!memory_mapped_from_zero) {
855 > #ifndef PAGEZERO_HACK
856 >                // Create Low Memory area (0x0000..0x3000)
857 >                if (vm_mac_acquire_fixed(0, 0x3000) < 0) {
858 >                        sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
859 >                        ErrorAlert(str);
860 >                        goto quit;
861 >                }
862 >                lm_area_mapped = true;
863 > #endif
864 > #if REAL_ADDRESSING
865 >                // Allocate RAM at any address. Since ROM must be higher than RAM, allocate the RAM
866 >                // and ROM areas contiguously, plus a little extra to allow for ROM address alignment.
867 >                RAMBaseHost = vm_mac_acquire(RAMSize + ROM_AREA_SIZE + ROM_ALIGNMENT);
868 >                if (RAMBaseHost == VM_MAP_FAILED) {
869 >                        sprintf(str, GetString(STR_RAM_ROM_MMAP_ERR), strerror(errno));
870 >                        ErrorAlert(str);
871 >                        goto quit;
872 >                }
873 >                RAMBase = Host2MacAddr(RAMBaseHost);
874 >                ROMBase = (RAMBase + RAMSize + ROM_ALIGNMENT -1) & -ROM_ALIGNMENT;
875 >                ROMBaseHost = Mac2HostAddr(ROMBase);
876 >                ram_rom_areas_contiguous = true;
877 > #else
878 >                if (vm_mac_acquire_fixed(RAM_BASE, RAMSize) < 0) {
879 >                        sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
880 >                        ErrorAlert(str);
881 >                        goto quit;
882 >                }
883 >                RAMBase = RAM_BASE;
884 >                RAMBaseHost = Mac2HostAddr(RAMBase);
885 > #endif
886          }
887   #if !EMULATED_PPC
888          if (vm_protect(RAMBaseHost, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
# Line 867 | Line 891 | int main(int argc, char **argv)
891                  goto quit;
892          }
893   #endif
870        RAMBase = RAM_BASE;
894          ram_area_mapped = true;
895          D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase));
896  
897 <        if (RAMBase > ROM_BASE) {
897 >        if (RAMBase > KernelDataAddr) {
898 >                ErrorAlert(GetString(STR_RAM_AREA_TOO_HIGH_ERR));
899 >                goto quit;
900 >        }
901 >        
902 >        // Create area for Mac ROM
903 >        if (!ram_rom_areas_contiguous) {
904 >                if (vm_mac_acquire_fixed(ROM_BASE, ROM_AREA_SIZE) < 0) {
905 >                        sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
906 >                        ErrorAlert(str);
907 >                        goto quit;
908 >                }
909 >                ROMBase = ROM_BASE;
910 >                ROMBaseHost = Mac2HostAddr(ROMBase);
911 >        }
912 > #if !EMULATED_PPC
913 >        if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
914 >                sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
915 >                ErrorAlert(str);
916 >                goto quit;
917 >        }
918 > #endif
919 >        rom_area_mapped = true;
920 >        D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROMBase));
921 >
922 >        if (RAMBase > ROMBase) {
923                  ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR));
924                  goto quit;
925          }
926  
927          // Load Mac ROM
928          rom_path = PrefsFindString("rom");
929 <        rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
929 >        rom_fd = open(rom_path && *rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
930          if (rom_fd < 0) {
931 <                rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME2, O_RDONLY);
931 >                rom_fd = open(ROM_FILE_NAME2, O_RDONLY);
932                  if (rom_fd < 0) {
933                          ErrorAlert(GetString(STR_NO_ROM_FILE_ERR));
934                          goto quit;
935                  }
936          }
937 <        printf(GetString(STR_READING_ROM_FILE));
937 >        printf("%s", GetString(STR_READING_ROM_FILE));
938          rom_size = lseek(rom_fd, 0, SEEK_END);
939          lseek(rom_fd, 0, SEEK_SET);
940          rom_tmp = new uint8[ROM_SIZE];
# Line 905 | Line 953 | int main(int argc, char **argv)
953          }
954          delete[] rom_tmp;
955  
956 <        // Load NVRAM
957 <        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));
956 >        // Initialize everything
957 >        if (!InitAll(vmdir))
958                  goto quit;
959 <        }
959 >        D(bug("Initialization complete\n"));
960  
961          // Clear caches (as we loaded and patched code) and write protect ROM
962   #if !EMULATED_PPC
963 <        MakeExecutable(0, ROM_BASE, ROM_AREA_SIZE);
963 >        flush_icache_range(ROMBase, ROMBase + ROM_AREA_SIZE);
964   #endif
965          vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE);
966  
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
967          // Start 60Hz thread
968          tick_thread_cancel = false;
969          tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0);
# Line 1088 | Line 985 | int main(int argc, char **argv)
985          sigill_action.sa_restorer = NULL;
986   #endif
987          if (sigaction(SIGILL, &sigill_action, NULL) < 0) {
988 <                sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno));
988 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno));
989                  ErrorAlert(str);
990                  goto quit;
991          }
# Line 1097 | Line 994 | int main(int argc, char **argv)
994   #if !EMULATED_PPC
995          // Install interrupt signal handler
996          sigemptyset(&sigusr2_action.sa_mask);
997 <        sigusr2_action.sa_sigaction = sigusr2_handler;
997 >        sigusr2_action.sa_sigaction = sigusr2_handler_init;
998          sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO;
999   #ifdef HAVE_SIGNAL_SA_RESTORER
1000          sigusr2_action.sa_restorer = NULL;
1001   #endif
1002          if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) {
1003 <                sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno));
1003 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGUSR2", strerror(errno));
1004                  ErrorAlert(str);
1005                  goto quit;
1006          }
# Line 1160 | Line 1057 | static void Quit(void)
1057          sigaction(SIGILL, &sigill_action, NULL);
1058  
1059          // Delete stacks for signal handlers
1060 <        for (int i = 0; i < SIG_STACK_COUNT; i++) {
1061 <                void *sig_stack = sig_stacks[i].ss_sp;
1062 <                if (sig_stack)
1063 <                        free(sig_stack);
1167 <        }
1060 >        if (sig_stack.ss_sp)
1061 >                free(sig_stack.ss_sp);
1062 >        if (extra_stack.ss_sp)
1063 >                free(extra_stack.ss_sp);
1064   #endif
1065  
1066 <        // Save NVRAM
1067 <        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();
1066 >        // Deinitialize everything
1067 >        ExitAll();
1068  
1069          // Delete SheepShaver globals
1070          SheepMem::Exit();
1071  
1072          // Delete RAM area
1073          if (ram_area_mapped)
1074 <                vm_release(RAMBaseHost, RAMSize);
1074 >                vm_mac_release(RAMBase, RAMSize);
1075  
1076          // Delete ROM area
1077          if (rom_area_mapped)
1078 <                vm_release(ROMBaseHost, ROM_AREA_SIZE);
1078 >                vm_mac_release(ROMBase, ROM_AREA_SIZE);
1079  
1080          // Delete DR cache areas
1081          if (dr_emulator_area_mapped)
1082 <                vm_release((void *)(DR_EMULATOR_BASE + NATMEM_OFFSET), DR_EMULATOR_SIZE);
1082 >                vm_mac_release(DR_EMULATOR_BASE, DR_EMULATOR_SIZE);
1083          if (dr_cache_area_mapped)
1084 <                vm_release((void *)(DR_CACHE_BASE + NATMEM_OFFSET), DR_CACHE_SIZE);
1084 >                vm_mac_release(DR_CACHE_BASE, DR_CACHE_SIZE);
1085  
1086          // Delete Kernel Data area
1087 <        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 <        }
1087 >        kernel_data_exit();
1088  
1089          // Delete Low Memory area
1090          if (lm_area_mapped)
1091 <                vm_release((void *)NATMEM_OFFSET, 0x3000);
1091 >                vm_mac_release(0, 0x3000);
1092  
1093          // Close /dev/zero
1094          if (zero_fd > 0)
# Line 1252 | Line 1111 | static void Quit(void)
1111                  XCloseDisplay(x_display);
1112   #endif
1113  
1114 +        // Notify GUI we are about to leave
1115 +        if (gui_connection) {
1116 +                if (rpc_method_invoke(gui_connection, RPC_METHOD_EXIT, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR)
1117 +                        rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID);
1118 +        }
1119 +
1120          exit(0);
1121   }
1122  
1123  
1124   /*
1125 + *  Initialize Kernel Data segments
1126 + */
1127 +
1128 + static bool kernel_data_init(void)
1129 + {
1130 +        char str[256];
1131 +        uint32 kernel_area_size = (KERNEL_AREA_SIZE + SHMLBA - 1) & -SHMLBA;
1132 +
1133 +        kernel_area = shmget(IPC_PRIVATE, kernel_area_size, 0600);
1134 +        if (kernel_area == -1) {
1135 +                sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno));
1136 +                ErrorAlert(str);
1137 +                return false;
1138 +        }
1139 +        void *kernel_addr = Mac2HostAddr(KERNEL_DATA_BASE & -SHMLBA);
1140 +        if (shmat(kernel_area, kernel_addr, 0) != kernel_addr) {
1141 +                sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno));
1142 +                ErrorAlert(str);
1143 +                return false;
1144 +        }
1145 +        kernel_addr = Mac2HostAddr(KERNEL_DATA2_BASE & -SHMLBA);
1146 +        if (shmat(kernel_area, kernel_addr, 0) != kernel_addr) {
1147 +                sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno));
1148 +                ErrorAlert(str);
1149 +                return false;
1150 +        }
1151 +        return true;
1152 + }
1153 +
1154 +
1155 + /*
1156 + *  Deallocate Kernel Data segments
1157 + */
1158 +
1159 + static void kernel_data_exit(void)
1160 + {
1161 +        if (kernel_area >= 0) {
1162 +                shmdt(Mac2HostAddr(KERNEL_DATA_BASE & -SHMLBA));
1163 +                shmdt(Mac2HostAddr(KERNEL_DATA2_BASE & -SHMLBA));
1164 +                shmctl(kernel_area, IPC_RMID, NULL);
1165 +        }
1166 + }
1167 +
1168 +
1169 + /*
1170   *  Jump into Mac ROM, start 680x0 emulator
1171   */
1172  
# Line 1284 | Line 1194 | static void *emul_func(void *arg)
1194          // Jump to ROM boot routine
1195          D(bug("Jumping to ROM\n"));
1196   #if EMULATED_PPC
1197 <        jump_to_rom(ROM_BASE + 0x310000);
1197 >        jump_to_rom(ROMBase + 0x310000);
1198   #else
1199 <        jump_to_rom(ROM_BASE + 0x310000, (uint32)emulator_data);
1199 >        jump_to_rom(ROMBase + 0x310000, (uint32)emulator_data);
1200   #endif
1201          D(bug("Returned from ROM\n"));
1202  
# Line 1343 | Line 1253 | void QuitEmulator(void)
1253  
1254  
1255   /*
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 /*
1256   *  Dump 68k registers
1257   */
1258  
# Line 1387 | Line 1282 | void Dump68kRegs(M68kRegisters *r)
1282  
1283   void MakeExecutable(int dummy, uint32 start, uint32 length)
1284   {
1285 <        if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE)))
1285 >        if ((start >= ROMBase) && (start < (ROMBase + ROM_SIZE)))
1286                  return;
1287   #if EMULATED_PPC
1288          FlushCodeCache(start, start + length);
1289   #else
1290 <        flush_icache_range(start, (void *)(start + length));
1290 >        flush_icache_range(start, start + length);
1291   #endif
1292   }
1293  
1294  
1295   /*
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 /*
1296   *  NVRAM watchdog thread (saves NVRAM every minute)
1297   */
1298  
# Line 1511 | Line 1395 | static void *tick_func(void *arg)
1395          }
1396  
1397          uint64 end = GetTicks_usec();
1398 <        D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1398 >        D(bug("%lld ticks in %lld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1399          return NULL;
1400   }
1401  
# Line 1561 | Line 1445 | struct B2_mutex {
1445              pthread_mutexattr_init(&attr);
1446              // Initialize the mutex for priority inheritance --
1447              // required for accurate timing.
1448 < #ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL
1448 > #if defined(HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL) && !defined(__CYGWIN__)
1449              pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
1450   #endif
1451   #if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL)
# Line 1635 | Line 1519 | void B2_delete_mutex(B2_mutex *mutex)
1519   #if !EMULATED_PPC
1520   void TriggerInterrupt(void)
1521   {
1522 <        if (ready_for_signals)
1522 >        if (ready_for_signals) {
1523 >                idle_resume();
1524                  pthread_kill(emul_thread, SIGUSR2);
1525 +        }
1526   }
1527   #endif
1528  
# Line 1691 | Line 1577 | void EnableInterrupt(void)
1577   */
1578  
1579   #if !EMULATED_PPC
1580 < static void sigusr2_handler(int sig, siginfo_t *sip, void *scp)
1580 > void sigusr2_handler(int sig, siginfo_t *sip, void *scp)
1581   {
1582          machine_regs *r = MACHINE_REGISTERS(scp);
1583  
1584 + #ifdef SYSTEM_CLOBBERS_R2
1585 +        // Restore pointer to Thread Local Storage
1586 +        set_r2(TOC);
1587 + #endif
1588 + #ifdef SYSTEM_CLOBBERS_R13
1589 +        // Restore pointer to .sdata section
1590 +        set_r13(R13);
1591 + #endif
1592 +
1593   #ifdef USE_SDL_VIDEO
1594          // We must fill in the events queue in the same thread that did call SDL_SetVideoMode()
1595          SDL_PumpEvents();
# Line 1720 | Line 1615 | static void sigusr2_handler(int sig, sig
1615                          // 68k emulator inactive, in nanokernel?
1616                          if (r->gpr(1) != KernelDataAddr) {
1617  
1618 <                                // Set extra stack for nested interrupts
1619 <                                sig_stack_acquire();
1618 >                                // Set extra stack for SIGSEGV handler
1619 >                                sigaltstack(&extra_stack, NULL);
1620                                  
1621                                  // Prepare for 68k interrupt level 1
1622                                  WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1);
# Line 1730 | Line 1625 | static void sigusr2_handler(int sig, sig
1625                                  // Execute nanokernel interrupt routine (this will activate the 68k emulator)
1626                                  DisableInterrupt();
1627                                  if (ROMType == ROMTYPE_NEWWORLD)
1628 <                                        ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr);
1628 >                                        ppc_interrupt(ROMBase + 0x312b1c, KernelDataAddr);
1629                                  else
1630 <                                        ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr);
1630 >                                        ppc_interrupt(ROMBase + 0x312a3c, KernelDataAddr);
1631  
1632 <                                // Reset normal signal stack
1633 <                                sig_stack_release();
1632 >                                // Reset normal stack
1633 >                                sigaltstack(&sig_stack, NULL);
1634                          }
1635                          break;
1636   #endif
# Line 1746 | Line 1641 | static void sigusr2_handler(int sig, sig
1641                          if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
1642  
1643                                  // Set extra stack for SIGSEGV handler
1644 <                                sig_stack_acquire();
1644 >                                sigaltstack(&extra_stack, NULL);
1645   #if 1
1646                                  // Execute full 68k interrupt routine
1647                                  M68kRegisters r;
# Line 1772 | Line 1667 | static void sigusr2_handler(int sig, sig
1667                                          }
1668                                  }
1669   #endif
1670 <                                // Reset normal signal stack
1671 <                                sig_stack_release();
1670 >                                // Reset normal stack
1671 >                                sigaltstack(&sig_stack, NULL);
1672                          }
1673                          break;
1674   #endif
# Line 1794 | Line 1689 | static void sigsegv_handler(int sig, sig
1689          // Get effective address
1690          uint32 addr = r->dar();
1691          
1692 + #ifdef SYSTEM_CLOBBERS_R2
1693 +        // Restore pointer to Thread Local Storage
1694 +        set_r2(TOC);
1695 + #endif
1696 + #ifdef SYSTEM_CLOBBERS_R13
1697 +        // Restore pointer to .sdata section
1698 +        set_r13(R13);
1699 + #endif
1700 +
1701   #if ENABLE_VOSF
1702 <        // Handle screen fault.
1703 <        extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction);
1704 <        if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc()))
1702 >        // Handle screen fault
1703 > #if SIGSEGV_CHECK_VERSION(1,0,0)
1704 >        sigsegv_info_t si;
1705 >        si.addr = (sigsegv_address_t)addr;
1706 >        si.pc = (sigsegv_address_t)r->pc();
1707 > #endif
1708 >        extern bool Screen_fault_handler(sigsegv_info_t *sip);
1709 >        if (Screen_fault_handler(&si))
1710                  return;
1711   #endif
1712  
1713          num_segv++;
1714  
1715          // Fault in Mac ROM or RAM or DR Cache?
1716 <        bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)) || (r->pc() >= DR_CACHE_BASE && r->pc() < (DR_CACHE_BASE + DR_CACHE_SIZE));
1716 >        bool mac_fault = (r->pc() >= ROMBase) && (r->pc() < (ROMBase + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)) || (r->pc() >= DR_CACHE_BASE && r->pc() < (DR_CACHE_BASE + DR_CACHE_SIZE));
1717          if (mac_fault) {
1718  
1719                  // "VM settings" during MacOS 8 installation
1720 <                if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) {
1720 >                if (r->pc() == ROMBase + 0x488160 && r->gpr(20) == 0xf8000000) {
1721                          r->pc() += 4;
1722                          r->gpr(8) = 0;
1723                          return;
1724          
1725                  // MacOS 8.5 installation
1726 <                } else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) {
1726 >                } else if (r->pc() == ROMBase + 0x488140 && r->gpr(16) == 0xf8000000) {
1727                          r->pc() += 4;
1728                          r->gpr(8) = 0;
1729                          return;
1730          
1731                  // MacOS 8 serial drivers on startup
1732 <                } else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) {
1732 >                } else if (r->pc() == ROMBase + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) {
1733                          r->pc() += 4;
1734                          r->gpr(8) = 0;
1735                          return;
1736          
1737                  // MacOS 8.1 serial drivers on startup
1738 <                } else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) {
1738 >                } else if (r->pc() == ROMBase + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) {
1739                          r->pc() += 4;
1740                          return;
1741 <                } else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) {
1741 >                } else if (r->pc() == ROMBase + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) {
1742                          r->pc() += 4;
1743                          return;
1744          
# Line 1962 | Line 1871 | static void sigsegv_handler(int sig, sig
1871          
1872                  // Ignore ROM writes (including to the zero page, which is read-only)
1873                  if (transfer_type == TYPE_STORE &&
1874 <                        ((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) ||
1874 >                        ((addr >= ROMBase && addr < ROMBase + ROM_SIZE) ||
1875                           (addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) {
1876   //                      D(bug("WARNING: %s write access to ROM at %08lx, pc %08lx\n", transfer_size == SIZE_BYTE ? "Byte" : transfer_size == SIZE_HALFWORD ? "Halfword" : "Word", addr, r->pc()));
1877                          if (addr_mode == MODE_U || addr_mode == MODE_UX)
# Line 2043 | Line 1952 | static void sigill_handler(int sig, sigi
1952          machine_regs *r = MACHINE_REGISTERS(scp);
1953          char str[256];
1954  
1955 + #ifdef SYSTEM_CLOBBERS_R2
1956 +        // Restore pointer to Thread Local Storage
1957 +        set_r2(TOC);
1958 + #endif
1959 + #ifdef SYSTEM_CLOBBERS_R13
1960 +        // Restore pointer to .sdata section
1961 +        set_r13(R13);
1962 + #endif
1963 +
1964          // Fault in Mac ROM or RAM?
1965 <        bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize));
1965 >        bool mac_fault = (r->pc() >= ROMBase) && (r->pc() < (ROMBase + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize));
1966          if (mac_fault) {
1967  
1968                  // Get opcode and divide into fields
# Line 2211 | Line 2129 | bool SheepMem::Init(void)
2129          page_size = getpagesize();
2130  
2131          // Allocate SheepShaver globals
2132 <        if (vm_acquire_fixed((char *)(base + NATMEM_OFFSET), size) < 0)
2132 >        proc = base;
2133 >        if (vm_mac_acquire_fixed(base, size) < 0)
2134                  return false;
2135  
2136 <        // Allocate page with all bits set to 0
2137 <        zero_page = base + size;
2138 <        uint8 *zero_page_host = (uint8 *)zero_page + NATMEM_OFFSET;
2139 <        if (vm_acquire_fixed(zero_page_host, page_size) < 0)
2140 <                return false;
2222 <        memset(zero_page_host, 0, page_size);
2223 <        if (vm_protect(zero_page_host, page_size, VM_PAGE_READ) < 0)
2136 >        // Allocate page with all bits set to 0, right in the middle
2137 >        // This is also used to catch undesired overlaps between proc and data areas
2138 >        zero_page = proc + (size / 2);
2139 >        Mac_memset(zero_page, 0, page_size);
2140 >        if (vm_protect(Mac2HostAddr(zero_page), page_size, VM_PAGE_READ) < 0)
2141                  return false;
2142  
2143   #if EMULATED_PPC
2144          // Allocate alternate stack for PowerPC interrupt routine
2145 <        sig_stack = zero_page + page_size;
2146 <        if (vm_acquire_fixed((char *)(sig_stack + NATMEM_OFFSET), SIG_STACK_SIZE) < 0)
2145 >        sig_stack = base + size;
2146 >        if (vm_mac_acquire_fixed(sig_stack, SIG_STACK_SIZE) < 0)
2147                  return false;
2148   #endif
2149  
2150 <        top = base + size;
2150 >        data = base + size;
2151          return true;
2152   }
2153  
2154   void SheepMem::Exit(void)
2155   {
2156 <        if (top) {
2156 >        if (data) {
2157                  // Delete SheepShaver globals
2158 <                vm_release((void *)(base + NATMEM_OFFSET), size);
2242 <
2243 <                // Delete zero page
2244 <                vm_release((void *)(zero_page + NATMEM_OFFSET), page_size);
2158 >                vm_mac_release(base, size);
2159  
2160   #if EMULATED_PPC
2161                  // Delete alternate stack for PowerPC interrupt routine
2162 <                vm_release((void *)(sig_stack + NATMEM_OFFSET), SIG_STACK_SIZE);
2162 >                vm_mac_release(sig_stack, SIG_STACK_SIZE);
2163   #endif
2164          }
2165   }
# Line 2300 | Line 2214 | void display_alert(int title_id, int pre
2214  
2215   void ErrorAlert(const char *text)
2216   {
2217 +        if (gui_connection) {
2218 +                if (rpc_method_invoke(gui_connection, RPC_METHOD_ERROR_ALERT, RPC_TYPE_STRING, text, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR &&
2219 +                        rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR)
2220 +                        return;
2221 +        }
2222   #if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO)
2223          if (PrefsFindBool("nogui") || x_display == NULL) {
2224                  printf(GetString(STR_SHELL_ERROR_PREFIX), text);
# Line 2319 | Line 2238 | void ErrorAlert(const char *text)
2238  
2239   void WarningAlert(const char *text)
2240   {
2241 +        if (gui_connection) {
2242 +                if (rpc_method_invoke(gui_connection, RPC_METHOD_WARNING_ALERT, RPC_TYPE_STRING, text, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR &&
2243 +                        rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR)
2244 +                        return;
2245 +        }
2246   #if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO)
2247          if (PrefsFindBool("nogui") || x_display == NULL) {
2248                  printf(GetString(STR_SHELL_WARNING_PREFIX), text);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines