107 |
|
#include "macos_util.h" |
108 |
|
#include "rom_patches.h" |
109 |
|
#include "user_strings.h" |
110 |
+ |
#include "vm_alloc.h" |
111 |
+ |
#include "sigsegv.h" |
112 |
+ |
#include "thunks.h" |
113 |
|
|
114 |
|
#define DEBUG 0 |
115 |
|
#include "debug.h" |
146 |
|
const char ROM_FILE_NAME[] = "ROM"; |
147 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
148 |
|
|
149 |
< |
const uint32 ROM_AREA_SIZE = 0x500000; // Size of ROM area |
147 |
< |
const uint32 ROM_END = ROM_BASE + ROM_SIZE; // End of ROM |
148 |
< |
|
149 |
< |
const uint32 KERNEL_DATA_BASE = 0x68ffe000; // Address of Kernel Data |
150 |
< |
const uint32 KERNEL_DATA2_BASE = 0x5fffe000; // Alternate address of Kernel Data |
151 |
< |
const uint32 KERNEL_AREA_SIZE = 0x2000; // Size of Kernel Data area |
152 |
< |
|
149 |
> |
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
150 |
|
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
151 |
|
|
152 |
|
|
156 |
– |
// 68k Emulator Data |
157 |
– |
struct EmulatorData { |
158 |
– |
uint32 v[0x400]; |
159 |
– |
}; |
160 |
– |
|
161 |
– |
|
162 |
– |
// Kernel Data |
163 |
– |
struct KernelData { |
164 |
– |
uint32 v[0x400]; |
165 |
– |
EmulatorData ed; |
166 |
– |
}; |
167 |
– |
|
168 |
– |
|
153 |
|
#if !EMULATED_PPC |
154 |
|
// Structure in which registers are saved in a signal handler; |
155 |
|
// sigcontext->regs points to it |
181 |
|
|
182 |
|
|
183 |
|
// Global variables |
184 |
< |
static char *x_display_name = NULL; // X11 display name |
184 |
> |
char *x_display_name = NULL; // X11 display name |
185 |
|
Display *x_display = NULL; // X11 display handle |
186 |
|
|
187 |
|
static int zero_fd = 0; // FD of /dev/zero |
189 |
|
static int kernel_area = -1; // SHM ID of Kernel Data area |
190 |
|
static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped |
191 |
|
static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped |
208 |
– |
static void *mmap_RAMBase = NULL; // Base address of mmap()ed RAM area |
192 |
|
static KernelData *kernel_data; // Pointer to Kernel Data |
193 |
|
static EmulatorData *emulator_data; |
194 |
|
|
203 |
|
static bool ready_for_signals = false; // Handler installed, signals can be sent |
204 |
|
static int64 num_segv = 0; // Number of handled SEGV signals |
205 |
|
|
223 |
– |
#if !EMULATED_PPC |
206 |
|
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
207 |
+ |
#if EMULATED_PPC |
208 |
+ |
static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine |
209 |
+ |
#else |
210 |
|
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
211 |
|
static struct sigaction sigill_action; // Illegal instruction signal (of emulator thread) |
212 |
|
static void *sig_stack = NULL; // Stack for signal handlers |
215 |
|
static sigregs sigsegv_regs; // Register dump when crashed |
216 |
|
#endif |
217 |
|
|
218 |
+ |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
219 |
+ |
uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data |
220 |
+ |
uintptr SheepMem::top = 0; // Top of SheepShaver data (stack like storage) |
221 |
+ |
|
222 |
|
|
223 |
|
// Prototypes |
224 |
|
static void Quit(void); |
225 |
|
static void *emul_func(void *arg); |
226 |
|
static void *nvram_func(void *arg); |
227 |
|
static void *tick_func(void *arg); |
228 |
< |
#if !EMULATED_PPC |
228 |
> |
#if EMULATED_PPC |
229 |
> |
static void sigusr2_handler(int sig); |
230 |
> |
extern void emul_ppc(uint32 start); |
231 |
> |
extern void init_emul_ppc(void); |
232 |
> |
extern void exit_emul_ppc(void); |
233 |
> |
#else |
234 |
|
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
235 |
|
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
236 |
|
static void sigill_handler(int sig, sigcontext_struct *sc); |
238 |
|
|
239 |
|
|
240 |
|
// From asm_linux.S |
241 |
< |
#if EMULATED_PPC |
248 |
< |
extern int atomic_add(int *var, int v); |
249 |
< |
extern int atomic_and(int *var, int v); |
250 |
< |
extern int atomic_or(int *var, int v); |
251 |
< |
#else |
241 |
> |
#if !EMULATED_PPC |
242 |
|
extern "C" void *get_toc(void); |
243 |
|
extern "C" void *get_sp(void); |
244 |
|
extern "C" void flush_icache_range(void *start, void *end); |
253 |
|
#endif |
254 |
|
|
255 |
|
|
256 |
< |
// Decode LZSS data |
257 |
< |
static void decode_lzss(const uint8 *src, uint8 *dest, int size) |
256 |
> |
#if EMULATED_PPC |
257 |
> |
/* |
258 |
> |
* Return signal stack base |
259 |
> |
*/ |
260 |
> |
|
261 |
> |
uintptr SignalStackBase(void) |
262 |
|
{ |
263 |
< |
char dict[0x1000]; |
270 |
< |
int run_mask = 0, dict_idx = 0xfee; |
271 |
< |
for (;;) { |
272 |
< |
if (run_mask < 0x100) { |
273 |
< |
// Start new run |
274 |
< |
if (--size < 0) |
275 |
< |
break; |
276 |
< |
run_mask = *src++ | 0xff00; |
277 |
< |
} |
278 |
< |
bool bit = run_mask & 1; |
279 |
< |
run_mask >>= 1; |
280 |
< |
if (bit) { |
281 |
< |
// Verbatim copy |
282 |
< |
if (--size < 0) |
283 |
< |
break; |
284 |
< |
int c = *src++; |
285 |
< |
dict[dict_idx++] = c; |
286 |
< |
*dest++ = c; |
287 |
< |
dict_idx &= 0xfff; |
288 |
< |
} else { |
289 |
< |
// Copy from dictionary |
290 |
< |
if (--size < 0) |
291 |
< |
break; |
292 |
< |
int idx = *src++; |
293 |
< |
if (--size < 0) |
294 |
< |
break; |
295 |
< |
int cnt = *src++; |
296 |
< |
idx |= (cnt << 4) & 0xf00; |
297 |
< |
cnt = (cnt & 0x0f) + 3; |
298 |
< |
while (cnt--) { |
299 |
< |
char c = dict[idx++]; |
300 |
< |
dict[dict_idx++] = c; |
301 |
< |
*dest++ = c; |
302 |
< |
idx &= 0xfff; |
303 |
< |
dict_idx &= 0xfff; |
304 |
< |
} |
305 |
< |
} |
306 |
< |
} |
263 |
> |
return sig_stack + SIG_STACK_SIZE; |
264 |
|
} |
265 |
|
|
266 |
|
|
267 |
|
/* |
268 |
+ |
* Atomic operations |
269 |
+ |
*/ |
270 |
+ |
|
271 |
+ |
#if HAVE_SPINLOCKS |
272 |
+ |
static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED; |
273 |
+ |
#else |
274 |
+ |
#define spin_lock(LOCK) |
275 |
+ |
#define spin_unlock(LOCK) |
276 |
+ |
#endif |
277 |
+ |
|
278 |
+ |
int atomic_add(int *var, int v) |
279 |
+ |
{ |
280 |
+ |
spin_lock(&atomic_ops_lock); |
281 |
+ |
int ret = *var; |
282 |
+ |
*var += v; |
283 |
+ |
spin_unlock(&atomic_ops_lock); |
284 |
+ |
return ret; |
285 |
+ |
} |
286 |
+ |
|
287 |
+ |
int atomic_and(int *var, int v) |
288 |
+ |
{ |
289 |
+ |
spin_lock(&atomic_ops_lock); |
290 |
+ |
int ret = *var; |
291 |
+ |
*var &= v; |
292 |
+ |
spin_unlock(&atomic_ops_lock); |
293 |
+ |
return ret; |
294 |
+ |
} |
295 |
+ |
|
296 |
+ |
int atomic_or(int *var, int v) |
297 |
+ |
{ |
298 |
+ |
spin_lock(&atomic_ops_lock); |
299 |
+ |
int ret = *var; |
300 |
+ |
*var |= v; |
301 |
+ |
spin_unlock(&atomic_ops_lock); |
302 |
+ |
return ret; |
303 |
+ |
} |
304 |
+ |
#endif |
305 |
+ |
|
306 |
+ |
|
307 |
+ |
/* |
308 |
|
* Main program |
309 |
|
*/ |
310 |
|
|
322 |
|
char str[256]; |
323 |
|
uint32 *boot_globs; |
324 |
|
int16 i16; |
328 |
– |
int drive, driver; |
325 |
|
int rom_fd; |
326 |
|
FILE *proc_file; |
327 |
|
const char *rom_path; |
331 |
|
|
332 |
|
// Initialize variables |
333 |
|
RAMBase = 0; |
338 |
– |
mmap_RAMBase = NULL; |
334 |
|
tzset(); |
335 |
|
|
336 |
|
// Print some info |
459 |
|
} |
460 |
|
|
461 |
|
// Create Low Memory area (0x0000..0x3000) |
462 |
< |
if (mmap((char *)0x0000, 0x3000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) == (void *)-1) { |
462 |
> |
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
463 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
464 |
|
ErrorAlert(str); |
465 |
|
goto quit; |
483 |
|
ErrorAlert(str); |
484 |
|
goto quit; |
485 |
|
} |
486 |
< |
kernel_data = (KernelData *)0x68ffe000; |
486 |
> |
kernel_data = (KernelData *)KERNEL_DATA_BASE; |
487 |
|
emulator_data = &kernel_data->ed; |
488 |
< |
KernelDataAddr = (uint32)kernel_data; |
488 |
> |
KernelDataAddr = KERNEL_DATA_BASE; |
489 |
|
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
490 |
|
|
491 |
+ |
// Create area for SheepShaver data |
492 |
+ |
if (!SheepMem::Init()) { |
493 |
+ |
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
494 |
+ |
ErrorAlert(str); |
495 |
+ |
goto quit; |
496 |
+ |
} |
497 |
+ |
|
498 |
|
// Create area for Mac ROM |
499 |
< |
if (mmap((char *)ROM_BASE, ROM_AREA_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) == (void *)-1) { |
499 |
> |
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
500 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
501 |
|
ErrorAlert(str); |
502 |
|
goto quit; |
503 |
|
} |
504 |
+ |
#if !EMULATED_PPC || defined(__powerpc__) |
505 |
+ |
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
506 |
+ |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
507 |
+ |
ErrorAlert(str); |
508 |
+ |
goto quit; |
509 |
+ |
} |
510 |
+ |
#endif |
511 |
|
rom_area_mapped = true; |
512 |
|
D(bug("ROM area at %08x\n", ROM_BASE)); |
513 |
|
|
518 |
|
RAMSize = 8*1024*1024; |
519 |
|
} |
520 |
|
|
521 |
< |
mmap_RAMBase = mmap((void *)0x20000000, RAMSize, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0); |
522 |
< |
if (mmap_RAMBase == (void *)-1) { |
521 |
> |
if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) { |
522 |
> |
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
523 |
> |
ErrorAlert(str); |
524 |
> |
goto quit; |
525 |
> |
} |
526 |
> |
#if !EMULATED_PPC |
527 |
> |
if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
528 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
529 |
|
ErrorAlert(str); |
530 |
|
goto quit; |
531 |
|
} |
532 |
< |
RAMBase = (uint32)mmap_RAMBase; |
532 |
> |
#endif |
533 |
> |
RAMBase = RAM_BASE; |
534 |
|
ram_area_mapped = true; |
535 |
|
D(bug("RAM area at %08x\n", RAMBase)); |
536 |
|
|
555 |
|
rom_tmp = new uint8[ROM_SIZE]; |
556 |
|
actual = read(rom_fd, (void *)rom_tmp, ROM_SIZE); |
557 |
|
close(rom_fd); |
558 |
< |
if (actual == ROM_SIZE) { |
559 |
< |
// Plain ROM image |
560 |
< |
memcpy((void *)ROM_BASE, rom_tmp, ROM_SIZE); |
561 |
< |
delete[] rom_tmp; |
547 |
< |
} else { |
548 |
< |
if (strncmp((char *)rom_tmp, "<CHRP-BOOT>", 11) == 0) { |
549 |
< |
// CHRP compressed ROM image |
550 |
< |
D(bug("CHRP ROM image\n")); |
551 |
< |
uint32 lzss_offset, lzss_size; |
552 |
< |
|
553 |
< |
char *s = strstr((char *)rom_tmp, "constant lzss-offset"); |
554 |
< |
if (s == NULL) { |
555 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
556 |
< |
goto quit; |
557 |
< |
} |
558 |
< |
s -= 7; |
559 |
< |
if (sscanf(s, "%06x", &lzss_offset) != 1) { |
560 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
561 |
< |
goto quit; |
562 |
< |
} |
563 |
< |
s = strstr((char *)rom_tmp, "constant lzss-size"); |
564 |
< |
if (s == NULL) { |
565 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
566 |
< |
goto quit; |
567 |
< |
} |
568 |
< |
s -= 7; |
569 |
< |
if (sscanf(s, "%06x", &lzss_size) != 1) { |
570 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
571 |
< |
goto quit; |
572 |
< |
} |
573 |
< |
D(bug("Offset of compressed data: %08x\n", lzss_offset)); |
574 |
< |
D(bug("Size of compressed data: %08x\n", lzss_size)); |
575 |
< |
|
576 |
< |
D(bug("Uncompressing ROM...\n")); |
577 |
< |
decode_lzss(rom_tmp + lzss_offset, (uint8 *)ROM_BASE, lzss_size); |
578 |
< |
delete[] rom_tmp; |
579 |
< |
} else if (rom_size != 4*1024*1024) { |
558 |
> |
|
559 |
> |
// Decode Mac ROM |
560 |
> |
if (!DecodeROM(rom_tmp, actual)) { |
561 |
> |
if (rom_size != 4*1024*1024) { |
562 |
|
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
563 |
|
goto quit; |
564 |
|
} else { |
566 |
|
goto quit; |
567 |
|
} |
568 |
|
} |
569 |
+ |
delete[] rom_tmp; |
570 |
|
|
571 |
|
// Load NVRAM |
572 |
|
XPRAMInit(); |
573 |
|
|
574 |
|
// Set boot volume |
575 |
< |
drive = PrefsFindInt32("bootdrive"); |
575 |
> |
i16 = PrefsFindInt32("bootdrive"); |
576 |
|
XPRAM[0x1378] = i16 >> 8; |
577 |
|
XPRAM[0x1379] = i16 & 0xff; |
578 |
< |
driver = PrefsFindInt32("bootdriver"); |
578 |
> |
i16 = PrefsFindInt32("bootdriver"); |
579 |
|
XPRAM[0x137a] = i16 >> 8; |
580 |
|
XPRAM[0x137b] = i16 & 0xff; |
581 |
|
|
588 |
|
boot_globs[1] = htonl(RAMSize); |
589 |
|
boot_globs[2] = htonl((uint32)-1); // End of bank table |
590 |
|
|
591 |
+ |
// Init thunks |
592 |
+ |
if (!ThunksInit()) |
593 |
+ |
goto quit; |
594 |
+ |
|
595 |
|
// Init drivers |
596 |
|
SonyInit(); |
597 |
|
DiskInit(); |
630 |
|
#if !EMULATED_PPC |
631 |
|
MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE); |
632 |
|
#endif |
633 |
< |
mprotect((char *)ROM_BASE, ROM_AREA_SIZE, PROT_EXEC | PROT_READ); |
633 |
> |
vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
634 |
|
|
635 |
|
// Initialize Kernel Data |
636 |
|
memset(kernel_data, 0, sizeof(KernelData)); |
637 |
|
if (ROMType == ROMTYPE_NEWWORLD) { |
638 |
< |
static uint32 of_dev_tree[4] = {0, 0, 0, 0}; |
639 |
< |
static uint8 vector_lookup_tbl[128]; |
640 |
< |
static uint8 vector_mask_tbl[64]; |
638 |
> |
uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32)); |
639 |
> |
memset((void *)of_dev_tree, 0, 4 * sizeof(uint32)); |
640 |
> |
uintptr vector_lookup_tbl = SheepMem::Reserve(128); |
641 |
> |
uintptr vector_mask_tbl = SheepMem::Reserve(64); |
642 |
|
memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80); |
643 |
< |
memset(vector_lookup_tbl, 0, 128); |
644 |
< |
memset(vector_mask_tbl, 0, 64); |
643 |
> |
memset((void *)vector_lookup_tbl, 0, 128); |
644 |
> |
memset((void *)vector_mask_tbl, 0, 64); |
645 |
|
kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE); |
646 |
< |
kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree); // OF device tree base |
647 |
< |
kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl); |
648 |
< |
kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl); |
646 |
> |
kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree); // OF device tree base |
647 |
> |
kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl); |
648 |
> |
kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl); |
649 |
|
kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base |
650 |
|
kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base |
651 |
|
kernel_data->v[0xc20 >> 2] = htonl(RAMSize); |
681 |
|
D(bug("Initializing Low Memory...\n")); |
682 |
|
memset(NULL, 0, 0x3000); |
683 |
|
WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver |
684 |
< |
WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data); // For trap replacement routines |
684 |
> |
WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr); // For trap replacement routines |
685 |
|
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
686 |
|
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
687 |
|
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
688 |
+ |
WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage()); // Pointer to read-only page with all bits set to 0 |
689 |
|
#if !EMULATED_PPC |
690 |
< |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
702 |
< |
WriteMacInt32(XLM_ETHER_INIT, (uint32)InitStreamModule); // DLPI ethernet driver functions |
703 |
< |
WriteMacInt32(XLM_ETHER_TERM, (uint32)TerminateStreamModule); |
704 |
< |
WriteMacInt32(XLM_ETHER_OPEN, (uint32)ether_open); |
705 |
< |
WriteMacInt32(XLM_ETHER_CLOSE, (uint32)ether_close); |
706 |
< |
WriteMacInt32(XLM_ETHER_WPUT, (uint32)ether_wput); |
707 |
< |
WriteMacInt32(XLM_ETHER_RSRV, (uint32)ether_rsrv); |
708 |
< |
WriteMacInt32(XLM_VIDEO_DOIO, (uint32)VideoDoDriverIO); |
690 |
> |
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
691 |
|
#endif |
692 |
+ |
WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT)); // DLPI ethernet driver functions |
693 |
+ |
WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM)); |
694 |
+ |
WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN)); |
695 |
+ |
WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE)); |
696 |
+ |
WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT)); |
697 |
+ |
WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV)); |
698 |
+ |
WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO)); |
699 |
|
D(bug("Low Memory initialized\n")); |
700 |
|
|
701 |
|
// Start 60Hz thread |
756 |
|
ErrorAlert(str); |
757 |
|
goto quit; |
758 |
|
} |
759 |
+ |
#endif |
760 |
|
|
761 |
|
// Install interrupt signal handler |
762 |
|
sigemptyset(&sigusr2_action.sa_mask); |
763 |
|
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
764 |
+ |
sigusr2_action.sa_flags = 0; |
765 |
+ |
#if !EMULATED_PPC |
766 |
|
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
767 |
+ |
#endif |
768 |
|
sigusr2_action.sa_restorer = NULL; |
769 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
770 |
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
771 |
|
ErrorAlert(str); |
772 |
|
goto quit; |
773 |
|
} |
781 |
– |
#endif |
774 |
|
|
775 |
|
// Get my thread ID and execute MacOS thread function |
776 |
|
emul_thread = pthread_self(); |
789 |
|
|
790 |
|
static void Quit(void) |
791 |
|
{ |
792 |
+ |
#if EMULATED_PPC |
793 |
+ |
// Exit PowerPC emulation |
794 |
+ |
exit_emul_ppc(); |
795 |
+ |
#endif |
796 |
+ |
|
797 |
|
// Stop 60Hz thread |
798 |
|
if (tick_thread_active) { |
799 |
|
pthread_cancel(tick_thread); |
850 |
|
DiskExit(); |
851 |
|
SonyExit(); |
852 |
|
|
853 |
+ |
// Delete SheepShaver globals |
854 |
+ |
SheepMem::Exit(); |
855 |
+ |
|
856 |
|
// Delete RAM area |
857 |
|
if (ram_area_mapped) |
858 |
< |
munmap(mmap_RAMBase, RAMSize); |
858 |
> |
vm_release((char *)RAM_BASE, RAMSize); |
859 |
|
|
860 |
|
// Delete ROM area |
861 |
|
if (rom_area_mapped) |
862 |
< |
munmap((char *)ROM_BASE, ROM_AREA_SIZE); |
862 |
> |
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
863 |
|
|
864 |
|
// Delete Kernel Data area |
865 |
|
if (kernel_area >= 0) { |
900 |
|
*/ |
901 |
|
|
902 |
|
#if EMULATED_PPC |
903 |
– |
extern void emul_ppc(uint32 start); |
904 |
– |
extern void init_emul_ppc(void); |
903 |
|
void jump_to_rom(uint32 entry) |
904 |
|
{ |
905 |
|
init_emul_ppc(); |
968 |
|
|
969 |
|
|
970 |
|
/* |
973 |
– |
* Execute PPC code from EMUL_OP routine (real mode switch) |
974 |
– |
*/ |
975 |
– |
|
976 |
– |
void ExecutePPC(void (*func)()) |
977 |
– |
{ |
978 |
– |
uint32 tvect[2] = {(uint32)func, 0}; // Fake TVECT |
979 |
– |
RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect); |
980 |
– |
M68kRegisters r; |
981 |
– |
Execute68k((uint32)&desc, &r); |
982 |
– |
} |
983 |
– |
|
984 |
– |
|
985 |
– |
/* |
971 |
|
* Quit emulator (cause return from jump_to_rom) |
972 |
|
*/ |
973 |
|
|
1026 |
|
|
1027 |
|
void MakeExecutable(int dummy, void *start, uint32 length) |
1028 |
|
{ |
1029 |
< |
#if !EMULATED_PPC |
1045 |
< |
if (((uint32)start >= ROM_BASE) && ((uint32)start < (ROM_BASE + ROM_SIZE))) |
1029 |
> |
if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE))) |
1030 |
|
return; |
1031 |
< |
flush_icache_range(start, (void *)((uint32)start + length)); |
1031 |
> |
#if EMULATED_PPC |
1032 |
> |
FlushCodeCache((uintptr)start, (uintptr)start + length); |
1033 |
> |
#else |
1034 |
> |
flush_icache_range(start, (void *)((uintptr)start + length)); |
1035 |
|
#endif |
1036 |
|
} |
1037 |
|
|
1042 |
|
|
1043 |
|
void PatchAfterStartup(void) |
1044 |
|
{ |
1045 |
< |
ExecutePPC(VideoInstallAccel); |
1045 |
> |
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1046 |
|
InstallExtFS(); |
1047 |
|
} |
1048 |
|
|
1145 |
|
|
1146 |
|
void Set_pthread_attr(pthread_attr_t *attr, int priority) |
1147 |
|
{ |
1148 |
< |
// nothing to do |
1148 |
> |
#ifdef HAVE_PTHREADS |
1149 |
> |
pthread_attr_init(attr); |
1150 |
> |
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
1151 |
> |
// Some of these only work for superuser |
1152 |
> |
if (geteuid() == 0) { |
1153 |
> |
pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED); |
1154 |
> |
pthread_attr_setschedpolicy(attr, SCHED_FIFO); |
1155 |
> |
struct sched_param fifo_param; |
1156 |
> |
fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) + |
1157 |
> |
sched_get_priority_max(SCHED_FIFO)) / 2 + |
1158 |
> |
priority); |
1159 |
> |
pthread_attr_setschedparam(attr, &fifo_param); |
1160 |
> |
} |
1161 |
> |
if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) { |
1162 |
> |
#ifdef PTHREAD_SCOPE_BOUND_NP |
1163 |
> |
// If system scope is not available (eg. we're not running |
1164 |
> |
// with CAP_SCHED_MGT capability on an SGI box), try bound |
1165 |
> |
// scope. It exposes pthread scheduling to the kernel, |
1166 |
> |
// without setting realtime priority. |
1167 |
> |
pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP); |
1168 |
> |
#endif |
1169 |
> |
} |
1170 |
> |
#endif |
1171 |
> |
#endif |
1172 |
|
} |
1173 |
|
|
1174 |
|
|
1176 |
|
* Mutexes |
1177 |
|
*/ |
1178 |
|
|
1179 |
+ |
#ifdef HAVE_PTHREADS |
1180 |
+ |
|
1181 |
+ |
struct B2_mutex { |
1182 |
+ |
B2_mutex() { |
1183 |
+ |
pthread_mutexattr_t attr; |
1184 |
+ |
pthread_mutexattr_init(&attr); |
1185 |
+ |
// Initialize the mutex for priority inheritance -- |
1186 |
+ |
// required for accurate timing. |
1187 |
+ |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL |
1188 |
+ |
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
1189 |
+ |
#endif |
1190 |
+ |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL) |
1191 |
+ |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); |
1192 |
+ |
#endif |
1193 |
+ |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPSHARED |
1194 |
+ |
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE); |
1195 |
+ |
#endif |
1196 |
+ |
pthread_mutex_init(&m, &attr); |
1197 |
+ |
pthread_mutexattr_destroy(&attr); |
1198 |
+ |
} |
1199 |
+ |
~B2_mutex() { |
1200 |
+ |
pthread_mutex_trylock(&m); // Make sure it's locked before |
1201 |
+ |
pthread_mutex_unlock(&m); // unlocking it. |
1202 |
+ |
pthread_mutex_destroy(&m); |
1203 |
+ |
} |
1204 |
+ |
pthread_mutex_t m; |
1205 |
+ |
}; |
1206 |
+ |
|
1207 |
+ |
B2_mutex *B2_create_mutex(void) |
1208 |
+ |
{ |
1209 |
+ |
return new B2_mutex; |
1210 |
+ |
} |
1211 |
+ |
|
1212 |
+ |
void B2_lock_mutex(B2_mutex *mutex) |
1213 |
+ |
{ |
1214 |
+ |
pthread_mutex_lock(&mutex->m); |
1215 |
+ |
} |
1216 |
+ |
|
1217 |
+ |
void B2_unlock_mutex(B2_mutex *mutex) |
1218 |
+ |
{ |
1219 |
+ |
pthread_mutex_unlock(&mutex->m); |
1220 |
+ |
} |
1221 |
+ |
|
1222 |
+ |
void B2_delete_mutex(B2_mutex *mutex) |
1223 |
+ |
{ |
1224 |
+ |
delete mutex; |
1225 |
+ |
} |
1226 |
+ |
|
1227 |
+ |
#else |
1228 |
+ |
|
1229 |
|
struct B2_mutex { |
1230 |
|
int dummy; |
1231 |
|
}; |
1248 |
|
delete mutex; |
1249 |
|
} |
1250 |
|
|
1251 |
+ |
#endif |
1252 |
+ |
|
1253 |
|
|
1254 |
|
/* |
1255 |
|
* Trigger signal USR2 from another thread |
1256 |
|
*/ |
1257 |
|
|
1258 |
+ |
#if !EMULATED_PPC || ASYNC_IRQ |
1259 |
|
void TriggerInterrupt(void) |
1260 |
|
{ |
1198 |
– |
#if EMULATED_PPC |
1199 |
– |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
1200 |
– |
#else |
1201 |
– |
#if 0 |
1202 |
– |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
1203 |
– |
#else |
1261 |
|
if (ready_for_signals) |
1262 |
|
pthread_kill(emul_thread, SIGUSR2); |
1206 |
– |
#endif |
1207 |
– |
#endif |
1263 |
|
} |
1264 |
+ |
#endif |
1265 |
|
|
1266 |
|
|
1267 |
|
/* |
1301 |
|
} |
1302 |
|
|
1303 |
|
|
1248 |
– |
#if !EMULATED_PPC |
1304 |
|
/* |
1305 |
|
* USR2 handler |
1306 |
|
*/ |
1307 |
|
|
1308 |
+ |
#if EMULATED_PPC |
1309 |
+ |
static void sigusr2_handler(int sig) |
1310 |
+ |
{ |
1311 |
+ |
#if ASYNC_IRQ |
1312 |
+ |
extern void HandleInterrupt(void); |
1313 |
+ |
HandleInterrupt(); |
1314 |
+ |
#endif |
1315 |
+ |
} |
1316 |
+ |
#else |
1317 |
|
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1318 |
|
{ |
1319 |
|
pt_regs *r = sc->regs; |
1383 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1384 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1385 |
|
ADBInterrupt(); |
1386 |
< |
ExecutePPC(VideoVBL); |
1386 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1387 |
|
} |
1388 |
|
} |
1389 |
|
#endif |
1395 |
|
} |
1396 |
|
break; |
1397 |
|
#endif |
1334 |
– |
|
1398 |
|
} |
1399 |
|
} |
1400 |
+ |
#endif |
1401 |
|
|
1402 |
|
|
1403 |
|
/* |
1404 |
|
* SIGSEGV handler |
1405 |
|
*/ |
1406 |
|
|
1407 |
+ |
#if !EMULATED_PPC |
1408 |
|
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1409 |
|
{ |
1410 |
|
pt_regs *r = sc->regs; |
1411 |
+ |
|
1412 |
+ |
// Get effective address |
1413 |
+ |
uint32 addr = r->dar; |
1414 |
+ |
|
1415 |
+ |
#if ENABLE_VOSF |
1416 |
+ |
// Handle screen fault. |
1417 |
+ |
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1418 |
+ |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip)) |
1419 |
+ |
return; |
1420 |
+ |
#endif |
1421 |
+ |
|
1422 |
|
num_segv++; |
1423 |
|
|
1424 |
|
// Fault in Mac ROM or RAM? |
1425 |
|
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1426 |
|
if (mac_fault) { |
1427 |
|
|
1352 |
– |
// Get opcode and divide into fields |
1353 |
– |
uint32 opcode = *((uint32 *)r->nip); |
1354 |
– |
uint32 primop = opcode >> 26; |
1355 |
– |
uint32 exop = (opcode >> 1) & 0x3ff; |
1356 |
– |
uint32 ra = (opcode >> 16) & 0x1f; |
1357 |
– |
uint32 rb = (opcode >> 11) & 0x1f; |
1358 |
– |
uint32 rd = (opcode >> 21) & 0x1f; |
1359 |
– |
int32 imm = (int16)(opcode & 0xffff); |
1360 |
– |
|
1428 |
|
// "VM settings" during MacOS 8 installation |
1429 |
|
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1430 |
|
r->nip += 4; |
1452 |
|
return; |
1453 |
|
} |
1454 |
|
|
1455 |
+ |
// Get opcode and divide into fields |
1456 |
+ |
uint32 opcode = *((uint32 *)r->nip); |
1457 |
+ |
uint32 primop = opcode >> 26; |
1458 |
+ |
uint32 exop = (opcode >> 1) & 0x3ff; |
1459 |
+ |
uint32 ra = (opcode >> 16) & 0x1f; |
1460 |
+ |
uint32 rb = (opcode >> 11) & 0x1f; |
1461 |
+ |
uint32 rd = (opcode >> 21) & 0x1f; |
1462 |
+ |
int32 imm = (int16)(opcode & 0xffff); |
1463 |
+ |
|
1464 |
|
// Analyze opcode |
1465 |
|
enum { |
1466 |
|
TYPE_UNKNOWN, |
1544 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1545 |
|
} |
1546 |
|
|
1471 |
– |
// Calculate effective address |
1472 |
– |
uint32 addr = 0; |
1473 |
– |
switch (addr_mode) { |
1474 |
– |
case MODE_X: |
1475 |
– |
case MODE_UX: |
1476 |
– |
if (ra == 0) |
1477 |
– |
addr = r->gpr[rb]; |
1478 |
– |
else |
1479 |
– |
addr = r->gpr[ra] + r->gpr[rb]; |
1480 |
– |
break; |
1481 |
– |
case MODE_NORM: |
1482 |
– |
case MODE_U: |
1483 |
– |
if (ra == 0) |
1484 |
– |
addr = (int32)(int16)imm; |
1485 |
– |
else |
1486 |
– |
addr = r->gpr[ra] + (int32)(int16)imm; |
1487 |
– |
break; |
1488 |
– |
default: |
1489 |
– |
break; |
1490 |
– |
} |
1491 |
– |
|
1547 |
|
// Ignore ROM writes |
1548 |
|
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1549 |
|
// 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->nip)); |
1780 |
|
|
1781 |
|
|
1782 |
|
/* |
1783 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
1784 |
+ |
*/ |
1785 |
+ |
|
1786 |
+ |
bool SheepMem::Init(void) |
1787 |
+ |
{ |
1788 |
+ |
const int page_size = getpagesize(); |
1789 |
+ |
|
1790 |
+ |
// Allocate SheepShaver globals |
1791 |
+ |
if (vm_acquire_fixed((char *)base, size) < 0) |
1792 |
+ |
return false; |
1793 |
+ |
|
1794 |
+ |
// Allocate page with all bits set to 0 |
1795 |
+ |
zero_page = base + size; |
1796 |
+ |
if (vm_acquire_fixed((char *)zero_page, page_size) < 0) |
1797 |
+ |
return false; |
1798 |
+ |
memset((char *)zero_page, 0, page_size); |
1799 |
+ |
if (vm_protect((char *)zero_page, page_size, VM_PAGE_READ) < 0) |
1800 |
+ |
return false; |
1801 |
+ |
|
1802 |
+ |
#if EMULATED_PPC |
1803 |
+ |
// Allocate alternate stack for PowerPC interrupt routine |
1804 |
+ |
sig_stack = zero_page + page_size; |
1805 |
+ |
if (vm_acquire_fixed((char *)sig_stack, SIG_STACK_SIZE) < 0) |
1806 |
+ |
return false; |
1807 |
+ |
#endif |
1808 |
+ |
|
1809 |
+ |
top = base + size; |
1810 |
+ |
return true; |
1811 |
+ |
} |
1812 |
+ |
|
1813 |
+ |
void SheepMem::Exit(void) |
1814 |
+ |
{ |
1815 |
+ |
if (top) { |
1816 |
+ |
const int page_size = getpagesize(); |
1817 |
+ |
|
1818 |
+ |
// Delete SheepShaver globals |
1819 |
+ |
vm_release((void *)base, size); |
1820 |
+ |
|
1821 |
+ |
// Delete zero page |
1822 |
+ |
vm_release((void *)zero_page, page_size); |
1823 |
+ |
|
1824 |
+ |
#if EMULATED_PPC |
1825 |
+ |
// Delete alternate stack for PowerPC interrupt routine |
1826 |
+ |
vm_release((void *)sig_stack, SIG_STACK_SIZE); |
1827 |
+ |
#endif |
1828 |
+ |
} |
1829 |
+ |
} |
1830 |
+ |
|
1831 |
+ |
|
1832 |
+ |
/* |
1833 |
|
* Display alert |
1834 |
|
*/ |
1835 |
|
|