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.92 by asvitkine, 2011-12-26T22:44:22Z

# 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, 0x00440000, "PPC970MP" },
689 +                        { 0xffff0000, 0x003a0000, "POWER5 (gr)" },
690 +                        { 0xffff0000, 0x003b0000, "POWER5+ (gs)" },
691 +                        { 0xffff0000, 0x003e0000, "POWER6" },
692 +                        { 0xffff0000, 0x00700000, "Cell Broadband Engine" },
693 +                        { 0x7fff0000, 0x00900000, "PA6T" },
694                          { 0, 0, 0 }
695                  };
696  
# Line 680 | Line 704 | int main(int argc, char **argv)
704  
705                          // Parse line
706                          int i;
707 +                        float f;
708                          char value[256];
709 <                        if (sscanf(line, "cpu : %[0-9A-Za-a]", value) == 1) {
709 >                        if (sscanf(line, "cpu : %[^,]", value) == 1) {
710                                  // Search by name
711                                  const char *cpu_name = NULL;
712                                  for (int i = 0; cpu_specs[i].pvr_mask != 0; i++) {
# Line 696 | Line 721 | int main(int argc, char **argv)
721                                  else
722                                          printf("Found a PowerPC %s processor\n", cpu_name);
723                          }
724 <                        if (sscanf(line, "clock : %dMHz", &i) == 1)
724 >                        if (sscanf(line, "clock : %fMHz", &f) == 1)
725 >                                CPUClockSpeed = BusClockSpeed = ((int64)f) * 1000000;
726 >                        else if (sscanf(line, "clock : %dMHz", &i) == 1)
727                                  CPUClockSpeed = BusClockSpeed = i * 1000000;
728                  }
729                  fclose(proc_file);
# Line 740 | Line 767 | int main(int argc, char **argv)
767          case 0x8000:                            // 7450
768          case 0x8001:                            // 7455
769          case 0x8002:                            // 7457
770 +        case 0x8003:                            // 7447A
771 +        case 0x8004:                            // 7448
772          case 0x0039:                            //  970
773 +        case 0x003c:                            //  970FX
774 +        case 0x0044:                            //  970MP
775                  PVR = 0x000c0000;               // 7400
776                  break;
777          }
# Line 767 | Line 798 | int main(int argc, char **argv)
798                  goto quit;
799          }
800  
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
801          // Create areas for Kernel Data
802 <        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);
802 >        if (!kernel_data_init())
803                  goto quit;
804 <        }
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);
804 >        kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE);
805          emulator_data = &kernel_data->ed;
806          KernelDataAddr = KERNEL_DATA_BASE;
807          D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE));
808          D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed)));
809  
810          // Create area for DR Cache
811 <        if (vm_acquire_fixed((void *)(DR_EMULATOR_BASE + NATMEM_OFFSET), DR_EMULATOR_SIZE) < 0) {
811 >        if (vm_mac_acquire_fixed(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) {
812                  sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno));
813                  ErrorAlert(str);
814                  goto quit;
815          }
816          dr_emulator_area_mapped = true;
817 <        if (vm_acquire_fixed((void *)(DR_CACHE_BASE + NATMEM_OFFSET), DR_CACHE_SIZE) < 0) {
817 >        if (vm_mac_acquire_fixed(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) {
818                  sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno));
819                  ErrorAlert(str);
820                  goto quit;
# Line 829 | Line 836 | int main(int argc, char **argv)
836                  ErrorAlert(str);
837                  goto quit;
838          }
839 <
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 <
839 >        
840          // Create area for Mac RAM
841          RAMSize = PrefsFindInt32("ramsize");
842          if (RAMSize < 8*1024*1024) {
843                  WarningAlert(GetString(STR_SMALL_RAM_WARN));
844                  RAMSize = 8*1024*1024;
845          }
846 <
847 <        RAMBaseHost = (uint8 *)(RAM_BASE + NATMEM_OFFSET);
848 <        if (vm_acquire_fixed(RAMBaseHost, RAMSize) < 0) {
849 <                sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
850 <                ErrorAlert(str);
851 <                goto quit;
846 >        memory_mapped_from_zero = false;
847 >        ram_rom_areas_contiguous = false;
848 > #if REAL_ADDRESSING && HAVE_LINKER_SCRIPT
849 >        if (vm_mac_acquire_fixed(0, RAMSize) == 0) {
850 >                D(bug("Could allocate RAM from 0x0000\n"));
851 >                RAMBase = 0;
852 >                RAMBaseHost = Mac2HostAddr(RAMBase);
853 >                memory_mapped_from_zero = true;
854 >        }
855 > #endif
856 >        if (!memory_mapped_from_zero) {
857 > #ifndef PAGEZERO_HACK
858 >                // Create Low Memory area (0x0000..0x3000)
859 >                if (vm_mac_acquire_fixed(0, 0x3000) < 0) {
860 >                        sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
861 >                        ErrorAlert(str);
862 >                        goto quit;
863 >                }
864 >                lm_area_mapped = true;
865 > #endif
866 > #if REAL_ADDRESSING
867 >                // Allocate RAM at any address. Since ROM must be higher than RAM, allocate the RAM
868 >                // and ROM areas contiguously, plus a little extra to allow for ROM address alignment.
869 >                RAMBaseHost = vm_mac_acquire(RAMSize + ROM_AREA_SIZE + ROM_ALIGNMENT);
870 >                if (RAMBaseHost == VM_MAP_FAILED) {
871 >                        sprintf(str, GetString(STR_RAM_ROM_MMAP_ERR), strerror(errno));
872 >                        ErrorAlert(str);
873 >                        goto quit;
874 >                }
875 >                RAMBase = Host2MacAddr(RAMBaseHost);
876 >                ROMBase = (RAMBase + RAMSize + ROM_ALIGNMENT -1) & -ROM_ALIGNMENT;
877 >                ROMBaseHost = Mac2HostAddr(ROMBase);
878 >                ram_rom_areas_contiguous = true;
879 > #else
880 >                if (vm_mac_acquire_fixed(RAM_BASE, RAMSize) < 0) {
881 >                        sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
882 >                        ErrorAlert(str);
883 >                        goto quit;
884 >                }
885 >                RAMBase = RAM_BASE;
886 >                RAMBaseHost = Mac2HostAddr(RAMBase);
887 > #endif
888          }
889   #if !EMULATED_PPC
890          if (vm_protect(RAMBaseHost, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
# Line 867 | Line 893 | int main(int argc, char **argv)
893                  goto quit;
894          }
895   #endif
870        RAMBase = RAM_BASE;
896          ram_area_mapped = true;
897          D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase));
898  
899 <        if (RAMBase > ROM_BASE) {
899 >        if (RAMBase > KernelDataAddr) {
900 >                ErrorAlert(GetString(STR_RAM_AREA_TOO_HIGH_ERR));
901 >                goto quit;
902 >        }
903 >        
904 >        // Create area for Mac ROM
905 >        if (!ram_rom_areas_contiguous) {
906 >                if (vm_mac_acquire_fixed(ROM_BASE, ROM_AREA_SIZE) < 0) {
907 >                        sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
908 >                        ErrorAlert(str);
909 >                        goto quit;
910 >                }
911 >                ROMBase = ROM_BASE;
912 >                ROMBaseHost = Mac2HostAddr(ROMBase);
913 >        }
914 > #if !EMULATED_PPC
915 >        if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
916 >                sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
917 >                ErrorAlert(str);
918 >                goto quit;
919 >        }
920 > #endif
921 >        rom_area_mapped = true;
922 >        D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROMBase));
923 >
924 >        if (RAMBase > ROMBase) {
925                  ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR));
926                  goto quit;
927          }
928  
929          // Load Mac ROM
930          rom_path = PrefsFindString("rom");
931 <        rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
931 >        rom_fd = open(rom_path && *rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
932          if (rom_fd < 0) {
933 <                rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME2, O_RDONLY);
933 >                rom_fd = open(ROM_FILE_NAME2, O_RDONLY);
934                  if (rom_fd < 0) {
935                          ErrorAlert(GetString(STR_NO_ROM_FILE_ERR));
936                          goto quit;
937                  }
938          }
939 <        printf(GetString(STR_READING_ROM_FILE));
939 >        printf("%s", GetString(STR_READING_ROM_FILE));
940          rom_size = lseek(rom_fd, 0, SEEK_END);
941          lseek(rom_fd, 0, SEEK_SET);
942          rom_tmp = new uint8[ROM_SIZE];
# Line 905 | Line 955 | int main(int argc, char **argv)
955          }
956          delete[] rom_tmp;
957  
958 <        // Load NVRAM
959 <        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));
958 >        // Initialize everything
959 >        if (!InitAll(vmdir))
960                  goto quit;
961 <        }
961 >        D(bug("Initialization complete\n"));
962  
963          // Clear caches (as we loaded and patched code) and write protect ROM
964   #if !EMULATED_PPC
965 <        MakeExecutable(0, ROM_BASE, ROM_AREA_SIZE);
965 >        flush_icache_range(ROMBase, ROMBase + ROM_AREA_SIZE);
966   #endif
967          vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE);
968  
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
969          // Start 60Hz thread
970          tick_thread_cancel = false;
971          tick_thread_active = (pthread_create(&tick_thread, NULL, tick_func, NULL) == 0);
# Line 1088 | Line 987 | int main(int argc, char **argv)
987          sigill_action.sa_restorer = NULL;
988   #endif
989          if (sigaction(SIGILL, &sigill_action, NULL) < 0) {
990 <                sprintf(str, GetString(STR_SIGILL_INSTALL_ERR), strerror(errno));
990 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno));
991                  ErrorAlert(str);
992                  goto quit;
993          }
# Line 1097 | Line 996 | int main(int argc, char **argv)
996   #if !EMULATED_PPC
997          // Install interrupt signal handler
998          sigemptyset(&sigusr2_action.sa_mask);
999 <        sigusr2_action.sa_sigaction = sigusr2_handler;
999 >        sigusr2_action.sa_sigaction = sigusr2_handler_init;
1000          sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO;
1001   #ifdef HAVE_SIGNAL_SA_RESTORER
1002          sigusr2_action.sa_restorer = NULL;
1003   #endif
1004          if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) {
1005 <                sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno));
1005 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGUSR2", strerror(errno));
1006                  ErrorAlert(str);
1007                  goto quit;
1008          }
# Line 1160 | Line 1059 | static void Quit(void)
1059          sigaction(SIGILL, &sigill_action, NULL);
1060  
1061          // Delete stacks for signal handlers
1062 <        for (int i = 0; i < SIG_STACK_COUNT; i++) {
1063 <                void *sig_stack = sig_stacks[i].ss_sp;
1064 <                if (sig_stack)
1065 <                        free(sig_stack);
1167 <        }
1062 >        if (sig_stack.ss_sp)
1063 >                free(sig_stack.ss_sp);
1064 >        if (extra_stack.ss_sp)
1065 >                free(extra_stack.ss_sp);
1066   #endif
1067  
1068 <        // Save NVRAM
1069 <        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();
1068 >        // Deinitialize everything
1069 >        ExitAll();
1070  
1071          // Delete SheepShaver globals
1072          SheepMem::Exit();
1073  
1074          // Delete RAM area
1075          if (ram_area_mapped)
1076 <                vm_release(RAMBaseHost, RAMSize);
1076 >                vm_mac_release(RAMBase, RAMSize);
1077  
1078          // Delete ROM area
1079          if (rom_area_mapped)
1080 <                vm_release(ROMBaseHost, ROM_AREA_SIZE);
1080 >                vm_mac_release(ROMBase, ROM_AREA_SIZE);
1081  
1082          // Delete DR cache areas
1083          if (dr_emulator_area_mapped)
1084 <                vm_release((void *)(DR_EMULATOR_BASE + NATMEM_OFFSET), DR_EMULATOR_SIZE);
1084 >                vm_mac_release(DR_EMULATOR_BASE, DR_EMULATOR_SIZE);
1085          if (dr_cache_area_mapped)
1086 <                vm_release((void *)(DR_CACHE_BASE + NATMEM_OFFSET), DR_CACHE_SIZE);
1086 >                vm_mac_release(DR_CACHE_BASE, DR_CACHE_SIZE);
1087  
1088          // Delete Kernel Data area
1089 <        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 <        }
1089 >        kernel_data_exit();
1090  
1091          // Delete Low Memory area
1092          if (lm_area_mapped)
1093 <                vm_release((void *)NATMEM_OFFSET, 0x3000);
1093 >                vm_mac_release(0, 0x3000);
1094  
1095          // Close /dev/zero
1096          if (zero_fd > 0)
# Line 1252 | Line 1113 | static void Quit(void)
1113                  XCloseDisplay(x_display);
1114   #endif
1115  
1116 +        // Notify GUI we are about to leave
1117 +        if (gui_connection) {
1118 +                if (rpc_method_invoke(gui_connection, RPC_METHOD_EXIT, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR)
1119 +                        rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID);
1120 +        }
1121 +
1122          exit(0);
1123   }
1124  
1125  
1126   /*
1127 + *  Initialize Kernel Data segments
1128 + */
1129 +
1130 + static bool kernel_data_init(void)
1131 + {
1132 +        char str[256];
1133 +        uint32 kernel_area_size = (KERNEL_AREA_SIZE + SHMLBA - 1) & -SHMLBA;
1134 +
1135 +        kernel_area = shmget(IPC_PRIVATE, kernel_area_size, 0600);
1136 +        if (kernel_area == -1) {
1137 +                sprintf(str, GetString(STR_KD_SHMGET_ERR), strerror(errno));
1138 +                ErrorAlert(str);
1139 +                return false;
1140 +        }
1141 +        void *kernel_addr = Mac2HostAddr(KERNEL_DATA_BASE & -SHMLBA);
1142 +        if (shmat(kernel_area, kernel_addr, 0) != kernel_addr) {
1143 +                sprintf(str, GetString(STR_KD_SHMAT_ERR), strerror(errno));
1144 +                ErrorAlert(str);
1145 +                return false;
1146 +        }
1147 +        kernel_addr = Mac2HostAddr(KERNEL_DATA2_BASE & -SHMLBA);
1148 +        if (shmat(kernel_area, kernel_addr, 0) != kernel_addr) {
1149 +                sprintf(str, GetString(STR_KD2_SHMAT_ERR), strerror(errno));
1150 +                ErrorAlert(str);
1151 +                return false;
1152 +        }
1153 +        return true;
1154 + }
1155 +
1156 +
1157 + /*
1158 + *  Deallocate Kernel Data segments
1159 + */
1160 +
1161 + static void kernel_data_exit(void)
1162 + {
1163 +        if (kernel_area >= 0) {
1164 +                shmdt(Mac2HostAddr(KERNEL_DATA_BASE & -SHMLBA));
1165 +                shmdt(Mac2HostAddr(KERNEL_DATA2_BASE & -SHMLBA));
1166 +                shmctl(kernel_area, IPC_RMID, NULL);
1167 +        }
1168 + }
1169 +
1170 +
1171 + /*
1172   *  Jump into Mac ROM, start 680x0 emulator
1173   */
1174  
# Line 1284 | Line 1196 | static void *emul_func(void *arg)
1196          // Jump to ROM boot routine
1197          D(bug("Jumping to ROM\n"));
1198   #if EMULATED_PPC
1199 <        jump_to_rom(ROM_BASE + 0x310000);
1199 >        jump_to_rom(ROMBase + 0x310000);
1200   #else
1201 <        jump_to_rom(ROM_BASE + 0x310000, (uint32)emulator_data);
1201 >        jump_to_rom(ROMBase + 0x310000, (uint32)emulator_data);
1202   #endif
1203          D(bug("Returned from ROM\n"));
1204  
# Line 1343 | Line 1255 | void QuitEmulator(void)
1255  
1256  
1257   /*
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 /*
1258   *  Dump 68k registers
1259   */
1260  
# Line 1387 | Line 1284 | void Dump68kRegs(M68kRegisters *r)
1284  
1285   void MakeExecutable(int dummy, uint32 start, uint32 length)
1286   {
1287 <        if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE)))
1287 >        if ((start >= ROMBase) && (start < (ROMBase + ROM_SIZE)))
1288                  return;
1289   #if EMULATED_PPC
1290          FlushCodeCache(start, start + length);
1291   #else
1292 <        flush_icache_range(start, (void *)(start + length));
1292 >        flush_icache_range(start, start + length);
1293   #endif
1294   }
1295  
1296  
1297   /*
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 /*
1298   *  NVRAM watchdog thread (saves NVRAM every minute)
1299   */
1300  
# Line 1511 | Line 1397 | static void *tick_func(void *arg)
1397          }
1398  
1399          uint64 end = GetTicks_usec();
1400 <        D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1400 >        D(bug("%lld ticks in %lld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1401          return NULL;
1402   }
1403  
# Line 1561 | Line 1447 | struct B2_mutex {
1447              pthread_mutexattr_init(&attr);
1448              // Initialize the mutex for priority inheritance --
1449              // required for accurate timing.
1450 < #ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL
1450 > #if defined(HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL) && !defined(__CYGWIN__)
1451              pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
1452   #endif
1453   #if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL)
# Line 1635 | Line 1521 | void B2_delete_mutex(B2_mutex *mutex)
1521   #if !EMULATED_PPC
1522   void TriggerInterrupt(void)
1523   {
1524 <        if (ready_for_signals)
1524 >        if (ready_for_signals) {
1525 >                idle_resume();
1526                  pthread_kill(emul_thread, SIGUSR2);
1527 +        }
1528   }
1529   #endif
1530  
# Line 1691 | Line 1579 | void EnableInterrupt(void)
1579   */
1580  
1581   #if !EMULATED_PPC
1582 < static void sigusr2_handler(int sig, siginfo_t *sip, void *scp)
1582 > void sigusr2_handler(int sig, siginfo_t *sip, void *scp)
1583   {
1584          machine_regs *r = MACHINE_REGISTERS(scp);
1585  
1586 + #ifdef SYSTEM_CLOBBERS_R2
1587 +        // Restore pointer to Thread Local Storage
1588 +        set_r2(TOC);
1589 + #endif
1590 + #ifdef SYSTEM_CLOBBERS_R13
1591 +        // Restore pointer to .sdata section
1592 +        set_r13(R13);
1593 + #endif
1594 +
1595   #ifdef USE_SDL_VIDEO
1596          // We must fill in the events queue in the same thread that did call SDL_SetVideoMode()
1597          SDL_PumpEvents();
# Line 1720 | Line 1617 | static void sigusr2_handler(int sig, sig
1617                          // 68k emulator inactive, in nanokernel?
1618                          if (r->gpr(1) != KernelDataAddr) {
1619  
1620 <                                // Set extra stack for nested interrupts
1621 <                                sig_stack_acquire();
1620 >                                // Set extra stack for SIGSEGV handler
1621 >                                sigaltstack(&extra_stack, NULL);
1622                                  
1623                                  // Prepare for 68k interrupt level 1
1624                                  WriteMacInt16(ntohl(kernel_data->v[0x67c >> 2]), 1);
# Line 1730 | Line 1627 | static void sigusr2_handler(int sig, sig
1627                                  // Execute nanokernel interrupt routine (this will activate the 68k emulator)
1628                                  DisableInterrupt();
1629                                  if (ROMType == ROMTYPE_NEWWORLD)
1630 <                                        ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr);
1630 >                                        ppc_interrupt(ROMBase + 0x312b1c, KernelDataAddr);
1631                                  else
1632 <                                        ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr);
1632 >                                        ppc_interrupt(ROMBase + 0x312a3c, KernelDataAddr);
1633  
1634 <                                // Reset normal signal stack
1635 <                                sig_stack_release();
1634 >                                // Reset normal stack
1635 >                                sigaltstack(&sig_stack, NULL);
1636                          }
1637                          break;
1638   #endif
# Line 1746 | Line 1643 | static void sigusr2_handler(int sig, sig
1643                          if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
1644  
1645                                  // Set extra stack for SIGSEGV handler
1646 <                                sig_stack_acquire();
1646 >                                sigaltstack(&extra_stack, NULL);
1647   #if 1
1648                                  // Execute full 68k interrupt routine
1649                                  M68kRegisters r;
# Line 1772 | Line 1669 | static void sigusr2_handler(int sig, sig
1669                                          }
1670                                  }
1671   #endif
1672 <                                // Reset normal signal stack
1673 <                                sig_stack_release();
1672 >                                // Reset normal stack
1673 >                                sigaltstack(&sig_stack, NULL);
1674                          }
1675                          break;
1676   #endif
# Line 1794 | Line 1691 | static void sigsegv_handler(int sig, sig
1691          // Get effective address
1692          uint32 addr = r->dar();
1693          
1694 + #ifdef SYSTEM_CLOBBERS_R2
1695 +        // Restore pointer to Thread Local Storage
1696 +        set_r2(TOC);
1697 + #endif
1698 + #ifdef SYSTEM_CLOBBERS_R13
1699 +        // Restore pointer to .sdata section
1700 +        set_r13(R13);
1701 + #endif
1702 +
1703   #if ENABLE_VOSF
1704 <        // Handle screen fault.
1705 <        extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction);
1706 <        if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->pc()))
1704 >        // Handle screen fault
1705 > #if SIGSEGV_CHECK_VERSION(1,0,0)
1706 >        sigsegv_info_t si;
1707 >        si.addr = (sigsegv_address_t)addr;
1708 >        si.pc = (sigsegv_address_t)r->pc();
1709 > #endif
1710 >        extern bool Screen_fault_handler(sigsegv_info_t *sip);
1711 >        if (Screen_fault_handler(&si))
1712                  return;
1713   #endif
1714  
1715          num_segv++;
1716  
1717          // Fault in Mac ROM or RAM or DR Cache?
1718 <        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));
1718 >        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));
1719          if (mac_fault) {
1720  
1721                  // "VM settings" during MacOS 8 installation
1722 <                if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) {
1722 >                if (r->pc() == ROMBase + 0x488160 && r->gpr(20) == 0xf8000000) {
1723                          r->pc() += 4;
1724                          r->gpr(8) = 0;
1725                          return;
1726          
1727                  // MacOS 8.5 installation
1728 <                } else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) {
1728 >                } else if (r->pc() == ROMBase + 0x488140 && r->gpr(16) == 0xf8000000) {
1729                          r->pc() += 4;
1730                          r->gpr(8) = 0;
1731                          return;
1732          
1733                  // MacOS 8 serial drivers on startup
1734 <                } else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) {
1734 >                } else if (r->pc() == ROMBase + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) {
1735                          r->pc() += 4;
1736                          r->gpr(8) = 0;
1737                          return;
1738          
1739                  // MacOS 8.1 serial drivers on startup
1740 <                } else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) {
1740 >                } else if (r->pc() == ROMBase + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) {
1741                          r->pc() += 4;
1742                          return;
1743 <                } else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) {
1743 >                } else if (r->pc() == ROMBase + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) {
1744                          r->pc() += 4;
1745                          return;
1746          
# Line 1962 | Line 1873 | static void sigsegv_handler(int sig, sig
1873          
1874                  // Ignore ROM writes (including to the zero page, which is read-only)
1875                  if (transfer_type == TYPE_STORE &&
1876 <                        ((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) ||
1876 >                        ((addr >= ROMBase && addr < ROMBase + ROM_SIZE) ||
1877                           (addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) {
1878   //                      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()));
1879                          if (addr_mode == MODE_U || addr_mode == MODE_UX)
# Line 2043 | Line 1954 | static void sigill_handler(int sig, sigi
1954          machine_regs *r = MACHINE_REGISTERS(scp);
1955          char str[256];
1956  
1957 + #ifdef SYSTEM_CLOBBERS_R2
1958 +        // Restore pointer to Thread Local Storage
1959 +        set_r2(TOC);
1960 + #endif
1961 + #ifdef SYSTEM_CLOBBERS_R13
1962 +        // Restore pointer to .sdata section
1963 +        set_r13(R13);
1964 + #endif
1965 +
1966          // Fault in Mac ROM or RAM?
1967 <        bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize));
1967 >        bool mac_fault = (r->pc() >= ROMBase) && (r->pc() < (ROMBase + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize));
1968          if (mac_fault) {
1969  
1970                  // Get opcode and divide into fields
# Line 2211 | Line 2131 | bool SheepMem::Init(void)
2131          page_size = getpagesize();
2132  
2133          // Allocate SheepShaver globals
2134 <        if (vm_acquire_fixed((char *)(base + NATMEM_OFFSET), size) < 0)
2134 >        proc = base;
2135 >        if (vm_mac_acquire_fixed(base, size) < 0)
2136                  return false;
2137  
2138 <        // Allocate page with all bits set to 0
2139 <        zero_page = base + size;
2140 <        uint8 *zero_page_host = (uint8 *)zero_page + NATMEM_OFFSET;
2141 <        if (vm_acquire_fixed(zero_page_host, page_size) < 0)
2142 <                return false;
2222 <        memset(zero_page_host, 0, page_size);
2223 <        if (vm_protect(zero_page_host, page_size, VM_PAGE_READ) < 0)
2138 >        // Allocate page with all bits set to 0, right in the middle
2139 >        // This is also used to catch undesired overlaps between proc and data areas
2140 >        zero_page = proc + (size / 2);
2141 >        Mac_memset(zero_page, 0, page_size);
2142 >        if (vm_protect(Mac2HostAddr(zero_page), page_size, VM_PAGE_READ) < 0)
2143                  return false;
2144  
2145   #if EMULATED_PPC
2146          // Allocate alternate stack for PowerPC interrupt routine
2147 <        sig_stack = zero_page + page_size;
2148 <        if (vm_acquire_fixed((char *)(sig_stack + NATMEM_OFFSET), SIG_STACK_SIZE) < 0)
2147 >        sig_stack = base + size;
2148 >        if (vm_mac_acquire_fixed(sig_stack, SIG_STACK_SIZE) < 0)
2149                  return false;
2150   #endif
2151  
2152 <        top = base + size;
2152 >        data = base + size;
2153          return true;
2154   }
2155  
2156   void SheepMem::Exit(void)
2157   {
2158 <        if (top) {
2158 >        if (data) {
2159                  // Delete SheepShaver globals
2160 <                vm_release((void *)(base + NATMEM_OFFSET), size);
2242 <
2243 <                // Delete zero page
2244 <                vm_release((void *)(zero_page + NATMEM_OFFSET), page_size);
2160 >                vm_mac_release(base, size);
2161  
2162   #if EMULATED_PPC
2163                  // Delete alternate stack for PowerPC interrupt routine
2164 <                vm_release((void *)(sig_stack + NATMEM_OFFSET), SIG_STACK_SIZE);
2164 >                vm_mac_release(sig_stack, SIG_STACK_SIZE);
2165   #endif
2166          }
2167   }
# Line 2300 | Line 2216 | void display_alert(int title_id, int pre
2216  
2217   void ErrorAlert(const char *text)
2218   {
2219 +        if (gui_connection) {
2220 +                if (rpc_method_invoke(gui_connection, RPC_METHOD_ERROR_ALERT, RPC_TYPE_STRING, text, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR &&
2221 +                        rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR)
2222 +                        return;
2223 +        }
2224   #if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO)
2225          if (PrefsFindBool("nogui") || x_display == NULL) {
2226                  printf(GetString(STR_SHELL_ERROR_PREFIX), text);
# Line 2319 | Line 2240 | void ErrorAlert(const char *text)
2240  
2241   void WarningAlert(const char *text)
2242   {
2243 +        if (gui_connection) {
2244 +                if (rpc_method_invoke(gui_connection, RPC_METHOD_WARNING_ALERT, RPC_TYPE_STRING, text, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR &&
2245 +                        rpc_method_wait_for_reply(gui_connection, RPC_TYPE_INVALID) == RPC_ERROR_NO_ERROR)
2246 +                        return;
2247 +        }
2248   #if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO)
2249          if (PrefsFindBool("nogui") || x_display == NULL) {
2250                  printf(GetString(STR_SHELL_WARNING_PREFIX), text);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines