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 |
|
|
113 |
|
#define DEBUG 0 |
114 |
|
#include "debug.h" |
145 |
|
const char ROM_FILE_NAME[] = "ROM"; |
146 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
147 |
|
|
148 |
< |
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 |
< |
|
148 |
> |
const uint32 RAM_BASE = 0x20000000; // Base address of RAM |
149 |
|
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
150 |
|
|
151 |
|
|
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 |
– |
|
152 |
|
#if !EMULATED_PPC |
153 |
|
// Structure in which registers are saved in a signal handler; |
154 |
|
// sigcontext->regs points to it |
172 |
|
#endif |
173 |
|
uint32 RAMBase; // Base address of Mac RAM |
174 |
|
uint32 RAMSize; // Size of Mac RAM |
175 |
+ |
uint32 SheepStack1Base; // SheepShaver first alternate stack base |
176 |
+ |
uint32 SheepStack2Base; // SheepShaver second alternate stack base |
177 |
+ |
uint32 SheepThunksBase; // SheepShaver thunks base |
178 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
179 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
180 |
|
uint32 PVR; // Theoretical PVR |
183 |
|
|
184 |
|
|
185 |
|
// Global variables |
186 |
< |
static char *x_display_name = NULL; // X11 display name |
186 |
> |
char *x_display_name = NULL; // X11 display name |
187 |
|
Display *x_display = NULL; // X11 display handle |
188 |
|
|
189 |
|
static int zero_fd = 0; // FD of /dev/zero |
190 |
+ |
static bool sheep_area_mapped = false; // Flag: SheepShaver data area mmap()ed |
191 |
|
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
192 |
|
static int kernel_area = -1; // SHM ID of Kernel Data area |
193 |
|
static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped |
194 |
|
static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped |
208 |
– |
static void *mmap_RAMBase = NULL; // Base address of mmap()ed RAM area |
195 |
|
static KernelData *kernel_data; // Pointer to Kernel Data |
196 |
|
static EmulatorData *emulator_data; |
197 |
|
|
206 |
|
static bool ready_for_signals = false; // Handler installed, signals can be sent |
207 |
|
static int64 num_segv = 0; // Number of handled SEGV signals |
208 |
|
|
223 |
– |
#if !EMULATED_PPC |
209 |
|
static struct sigaction sigusr2_action; // Interrupt signal (of emulator thread) |
210 |
+ |
#if !EMULATED_PPC |
211 |
|
static struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
212 |
|
static struct sigaction sigill_action; // Illegal instruction signal (of emulator thread) |
213 |
|
static void *sig_stack = NULL; // Stack for signal handlers |
222 |
|
static void *emul_func(void *arg); |
223 |
|
static void *nvram_func(void *arg); |
224 |
|
static void *tick_func(void *arg); |
225 |
< |
#if !EMULATED_PPC |
225 |
> |
#if EMULATED_PPC |
226 |
> |
static void sigusr2_handler(int sig); |
227 |
> |
extern void emul_ppc(uint32 start); |
228 |
> |
extern void init_emul_ppc(void); |
229 |
> |
extern void exit_emul_ppc(void); |
230 |
> |
#else |
231 |
|
static void sigusr2_handler(int sig, sigcontext_struct *sc); |
232 |
|
static void sigsegv_handler(int sig, sigcontext_struct *sc); |
233 |
|
static void sigill_handler(int sig, sigcontext_struct *sc); |
235 |
|
|
236 |
|
|
237 |
|
// From asm_linux.S |
238 |
< |
#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 |
238 |
> |
#if !EMULATED_PPC |
239 |
|
extern "C" void *get_toc(void); |
240 |
|
extern "C" void *get_sp(void); |
241 |
|
extern "C" void flush_icache_range(void *start, void *end); |
250 |
|
#endif |
251 |
|
|
252 |
|
|
253 |
< |
// Decode LZSS data |
254 |
< |
static void decode_lzss(const uint8 *src, uint8 *dest, int size) |
253 |
> |
#if EMULATED_PPC |
254 |
> |
/* |
255 |
> |
* Atomic operations |
256 |
> |
*/ |
257 |
> |
|
258 |
> |
#if HAVE_SPINLOCKS |
259 |
> |
static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED; |
260 |
> |
#else |
261 |
> |
#define spin_lock(LOCK) |
262 |
> |
#define spin_unlock(LOCK) |
263 |
> |
#endif |
264 |
> |
|
265 |
> |
int atomic_add(int *var, int v) |
266 |
|
{ |
267 |
< |
char dict[0x1000]; |
268 |
< |
int run_mask = 0, dict_idx = 0xfee; |
269 |
< |
for (;;) { |
270 |
< |
if (run_mask < 0x100) { |
271 |
< |
// Start new run |
272 |
< |
if (--size < 0) |
273 |
< |
break; |
274 |
< |
run_mask = *src++ | 0xff00; |
275 |
< |
} |
276 |
< |
bool bit = run_mask & 1; |
277 |
< |
run_mask >>= 1; |
278 |
< |
if (bit) { |
279 |
< |
// Verbatim copy |
280 |
< |
if (--size < 0) |
281 |
< |
break; |
282 |
< |
int c = *src++; |
283 |
< |
dict[dict_idx++] = c; |
284 |
< |
*dest++ = c; |
285 |
< |
dict_idx &= 0xfff; |
286 |
< |
} else { |
287 |
< |
// Copy from dictionary |
288 |
< |
if (--size < 0) |
289 |
< |
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 |
< |
} |
267 |
> |
spin_lock(&atomic_ops_lock); |
268 |
> |
int ret = *var; |
269 |
> |
*var += v; |
270 |
> |
spin_unlock(&atomic_ops_lock); |
271 |
> |
return ret; |
272 |
> |
} |
273 |
> |
|
274 |
> |
int atomic_and(int *var, int v) |
275 |
> |
{ |
276 |
> |
spin_lock(&atomic_ops_lock); |
277 |
> |
int ret = *var; |
278 |
> |
*var &= v; |
279 |
> |
spin_unlock(&atomic_ops_lock); |
280 |
> |
return ret; |
281 |
> |
} |
282 |
> |
|
283 |
> |
int atomic_or(int *var, int v) |
284 |
> |
{ |
285 |
> |
spin_lock(&atomic_ops_lock); |
286 |
> |
int ret = *var; |
287 |
> |
*var |= v; |
288 |
> |
spin_unlock(&atomic_ops_lock); |
289 |
> |
return ret; |
290 |
|
} |
291 |
+ |
#endif |
292 |
|
|
293 |
|
|
294 |
|
/* |
309 |
|
char str[256]; |
310 |
|
uint32 *boot_globs; |
311 |
|
int16 i16; |
328 |
– |
int drive, driver; |
312 |
|
int rom_fd; |
313 |
|
FILE *proc_file; |
314 |
|
const char *rom_path; |
318 |
|
|
319 |
|
// Initialize variables |
320 |
|
RAMBase = 0; |
338 |
– |
mmap_RAMBase = NULL; |
321 |
|
tzset(); |
322 |
|
|
323 |
|
// Print some info |
446 |
|
} |
447 |
|
|
448 |
|
// Create Low Memory area (0x0000..0x3000) |
449 |
< |
if (mmap((char *)0x0000, 0x3000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) == (void *)-1) { |
449 |
> |
if (vm_acquire_fixed((char *)0, 0x3000) < 0) { |
450 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
451 |
|
ErrorAlert(str); |
452 |
|
goto quit; |
475 |
|
KernelDataAddr = (uint32)kernel_data; |
476 |
|
D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data)); |
477 |
|
|
478 |
+ |
// Create area for SheepShaver data |
479 |
+ |
if (vm_acquire_fixed((char *)SHEEP_BASE, SHEEP_SIZE) < 0) { |
480 |
+ |
sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno)); |
481 |
+ |
ErrorAlert(str); |
482 |
+ |
goto quit; |
483 |
+ |
} |
484 |
+ |
SheepStack1Base = SHEEP_BASE + 0x10000; |
485 |
+ |
SheepStack2Base = SheepStack1Base + 0x10000; |
486 |
+ |
SheepThunksBase = SheepStack2Base + 0x1000; |
487 |
+ |
sheep_area_mapped = true; |
488 |
+ |
|
489 |
|
// Create area for Mac ROM |
490 |
< |
if (mmap((char *)ROM_BASE, ROM_AREA_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) == (void *)-1) { |
490 |
> |
if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) { |
491 |
> |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
492 |
> |
ErrorAlert(str); |
493 |
> |
goto quit; |
494 |
> |
} |
495 |
> |
#if !EMULATED_PPC || defined(__powerpc__) |
496 |
> |
if (vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
497 |
|
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
498 |
|
ErrorAlert(str); |
499 |
|
goto quit; |
500 |
|
} |
501 |
+ |
#endif |
502 |
|
rom_area_mapped = true; |
503 |
|
D(bug("ROM area at %08x\n", ROM_BASE)); |
504 |
|
|
509 |
|
RAMSize = 8*1024*1024; |
510 |
|
} |
511 |
|
|
512 |
< |
mmap_RAMBase = mmap((void *)0x20000000, RAMSize, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0); |
513 |
< |
if (mmap_RAMBase == (void *)-1) { |
512 |
> |
if (vm_acquire_fixed((char *)RAM_BASE, RAMSize) < 0) { |
513 |
> |
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
514 |
> |
ErrorAlert(str); |
515 |
> |
goto quit; |
516 |
> |
} |
517 |
> |
#if !EMULATED_PPC |
518 |
> |
if (vm_protect((char *)RAM_BASE, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
519 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
520 |
|
ErrorAlert(str); |
521 |
|
goto quit; |
522 |
|
} |
523 |
< |
RAMBase = (uint32)mmap_RAMBase; |
523 |
> |
#endif |
524 |
> |
RAMBase = RAM_BASE; |
525 |
|
ram_area_mapped = true; |
526 |
|
D(bug("RAM area at %08x\n", RAMBase)); |
527 |
|
|
546 |
|
rom_tmp = new uint8[ROM_SIZE]; |
547 |
|
actual = read(rom_fd, (void *)rom_tmp, ROM_SIZE); |
548 |
|
close(rom_fd); |
549 |
< |
if (actual == ROM_SIZE) { |
550 |
< |
// Plain ROM image |
551 |
< |
memcpy((void *)ROM_BASE, rom_tmp, ROM_SIZE); |
552 |
< |
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) { |
549 |
> |
|
550 |
> |
// Decode Mac ROM |
551 |
> |
if (!DecodeROM(rom_tmp, actual)) { |
552 |
> |
if (rom_size != 4*1024*1024) { |
553 |
|
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
554 |
|
goto quit; |
555 |
|
} else { |
557 |
|
goto quit; |
558 |
|
} |
559 |
|
} |
560 |
+ |
delete[] rom_tmp; |
561 |
|
|
562 |
|
// Load NVRAM |
563 |
|
XPRAMInit(); |
564 |
|
|
565 |
|
// Set boot volume |
566 |
< |
drive = PrefsFindInt32("bootdrive"); |
566 |
> |
i16 = PrefsFindInt32("bootdrive"); |
567 |
|
XPRAM[0x1378] = i16 >> 8; |
568 |
|
XPRAM[0x1379] = i16 & 0xff; |
569 |
< |
driver = PrefsFindInt32("bootdriver"); |
569 |
> |
i16 = PrefsFindInt32("bootdriver"); |
570 |
|
XPRAM[0x137a] = i16 >> 8; |
571 |
|
XPRAM[0x137b] = i16 & 0xff; |
572 |
|
|
617 |
|
#if !EMULATED_PPC |
618 |
|
MakeExecutable(0, (void *)ROM_BASE, ROM_AREA_SIZE); |
619 |
|
#endif |
620 |
< |
mprotect((char *)ROM_BASE, ROM_AREA_SIZE, PROT_EXEC | PROT_READ); |
620 |
> |
vm_protect((char *)ROM_BASE, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
621 |
|
|
622 |
|
// Initialize Kernel Data |
623 |
|
memset(kernel_data, 0, sizeof(KernelData)); |
671 |
|
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
672 |
|
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
673 |
|
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
674 |
< |
#if !EMULATED_PPC |
674 |
> |
#if EMULATED_PPC |
675 |
> |
WriteMacInt32(XLM_ETHER_INIT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_INIT)); |
676 |
> |
WriteMacInt32(XLM_ETHER_TERM, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_TERM)); |
677 |
> |
WriteMacInt32(XLM_ETHER_OPEN, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_OPEN)); |
678 |
> |
WriteMacInt32(XLM_ETHER_CLOSE, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_CLOSE)); |
679 |
> |
WriteMacInt32(XLM_ETHER_WPUT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_WPUT)); |
680 |
> |
WriteMacInt32(XLM_ETHER_RSRV, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_RSRV)); |
681 |
> |
WriteMacInt32(XLM_VIDEO_DOIO, POWERPC_NATIVE_OP_FUNC(NATIVE_VIDEO_DO_DRIVER_IO)); |
682 |
> |
#else |
683 |
|
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
684 |
|
WriteMacInt32(XLM_ETHER_INIT, (uint32)InitStreamModule); // DLPI ethernet driver functions |
685 |
|
WriteMacInt32(XLM_ETHER_TERM, (uint32)TerminateStreamModule); |
749 |
|
ErrorAlert(str); |
750 |
|
goto quit; |
751 |
|
} |
752 |
+ |
#endif |
753 |
|
|
754 |
|
// Install interrupt signal handler |
755 |
|
sigemptyset(&sigusr2_action.sa_mask); |
756 |
|
sigusr2_action.sa_handler = (__sighandler_t)sigusr2_handler; |
757 |
+ |
sigusr2_action.sa_flags = 0; |
758 |
+ |
#if !EMULATED_PPC |
759 |
|
sigusr2_action.sa_flags = SA_ONSTACK | SA_RESTART; |
760 |
+ |
#endif |
761 |
|
sigusr2_action.sa_restorer = NULL; |
762 |
|
if (sigaction(SIGUSR2, &sigusr2_action, NULL) < 0) { |
763 |
|
sprintf(str, GetString(STR_SIGUSR2_INSTALL_ERR), strerror(errno)); |
764 |
|
ErrorAlert(str); |
765 |
|
goto quit; |
766 |
|
} |
781 |
– |
#endif |
767 |
|
|
768 |
|
// Get my thread ID and execute MacOS thread function |
769 |
|
emul_thread = pthread_self(); |
782 |
|
|
783 |
|
static void Quit(void) |
784 |
|
{ |
785 |
+ |
#if EMULATED_PPC |
786 |
+ |
// Exit PowerPC emulation |
787 |
+ |
exit_emul_ppc(); |
788 |
+ |
#endif |
789 |
+ |
|
790 |
|
// Stop 60Hz thread |
791 |
|
if (tick_thread_active) { |
792 |
|
pthread_cancel(tick_thread); |
845 |
|
|
846 |
|
// Delete RAM area |
847 |
|
if (ram_area_mapped) |
848 |
< |
munmap(mmap_RAMBase, RAMSize); |
848 |
> |
vm_release((char *)RAM_BASE, RAMSize); |
849 |
|
|
850 |
|
// Delete ROM area |
851 |
|
if (rom_area_mapped) |
852 |
< |
munmap((char *)ROM_BASE, ROM_AREA_SIZE); |
852 |
> |
vm_release((char *)ROM_BASE, ROM_AREA_SIZE); |
853 |
|
|
854 |
|
// Delete Kernel Data area |
855 |
|
if (kernel_area >= 0) { |
890 |
|
*/ |
891 |
|
|
892 |
|
#if EMULATED_PPC |
903 |
– |
extern void emul_ppc(uint32 start); |
904 |
– |
extern void init_emul_ppc(void); |
893 |
|
void jump_to_rom(uint32 entry) |
894 |
|
{ |
895 |
|
init_emul_ppc(); |
954 |
|
uint16 proc[2] = {trap, M68K_RTS}; |
955 |
|
Execute68k((uint32)proc, r); |
956 |
|
} |
969 |
– |
#endif |
957 |
|
|
958 |
|
|
959 |
|
/* |
967 |
|
M68kRegisters r; |
968 |
|
Execute68k((uint32)&desc, &r); |
969 |
|
} |
970 |
+ |
#endif |
971 |
|
|
972 |
|
|
973 |
|
/* |
1029 |
|
|
1030 |
|
void MakeExecutable(int dummy, void *start, uint32 length) |
1031 |
|
{ |
1032 |
< |
#if !EMULATED_PPC |
1045 |
< |
if (((uint32)start >= ROM_BASE) && ((uint32)start < (ROM_BASE + ROM_SIZE))) |
1032 |
> |
if (((uintptr)start >= ROM_BASE) && ((uintptr)start < (ROM_BASE + ROM_SIZE))) |
1033 |
|
return; |
1034 |
< |
flush_icache_range(start, (void *)((uint32)start + length)); |
1034 |
> |
#if EMULATED_PPC |
1035 |
> |
FlushCodeCache((uintptr)start, (uintptr)start + length); |
1036 |
> |
#else |
1037 |
> |
flush_icache_range(start, (void *)((uintptr)start + length)); |
1038 |
|
#endif |
1039 |
|
} |
1040 |
|
|
1045 |
|
|
1046 |
|
void PatchAfterStartup(void) |
1047 |
|
{ |
1048 |
+ |
#if EMULATED_PPC |
1049 |
+ |
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1050 |
+ |
#else |
1051 |
|
ExecutePPC(VideoInstallAccel); |
1052 |
+ |
#endif |
1053 |
|
InstallExtFS(); |
1054 |
|
} |
1055 |
|
|
1160 |
|
* Mutexes |
1161 |
|
*/ |
1162 |
|
|
1163 |
+ |
#ifdef HAVE_PTHREADS |
1164 |
+ |
|
1165 |
+ |
struct B2_mutex { |
1166 |
+ |
B2_mutex() { |
1167 |
+ |
pthread_mutexattr_t attr; |
1168 |
+ |
pthread_mutexattr_init(&attr); |
1169 |
+ |
// Initialize the mutex for priority inheritance -- |
1170 |
+ |
// required for accurate timing. |
1171 |
+ |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL |
1172 |
+ |
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
1173 |
+ |
#endif |
1174 |
+ |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL) |
1175 |
+ |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); |
1176 |
+ |
#endif |
1177 |
+ |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPSHARED |
1178 |
+ |
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE); |
1179 |
+ |
#endif |
1180 |
+ |
pthread_mutex_init(&m, &attr); |
1181 |
+ |
pthread_mutexattr_destroy(&attr); |
1182 |
+ |
} |
1183 |
+ |
~B2_mutex() { |
1184 |
+ |
pthread_mutex_trylock(&m); // Make sure it's locked before |
1185 |
+ |
pthread_mutex_unlock(&m); // unlocking it. |
1186 |
+ |
pthread_mutex_destroy(&m); |
1187 |
+ |
} |
1188 |
+ |
pthread_mutex_t m; |
1189 |
+ |
}; |
1190 |
+ |
|
1191 |
+ |
B2_mutex *B2_create_mutex(void) |
1192 |
+ |
{ |
1193 |
+ |
return new B2_mutex; |
1194 |
+ |
} |
1195 |
+ |
|
1196 |
+ |
void B2_lock_mutex(B2_mutex *mutex) |
1197 |
+ |
{ |
1198 |
+ |
pthread_mutex_lock(&mutex->m); |
1199 |
+ |
} |
1200 |
+ |
|
1201 |
+ |
void B2_unlock_mutex(B2_mutex *mutex) |
1202 |
+ |
{ |
1203 |
+ |
pthread_mutex_unlock(&mutex->m); |
1204 |
+ |
} |
1205 |
+ |
|
1206 |
+ |
void B2_delete_mutex(B2_mutex *mutex) |
1207 |
+ |
{ |
1208 |
+ |
delete mutex; |
1209 |
+ |
} |
1210 |
+ |
|
1211 |
+ |
#else |
1212 |
+ |
|
1213 |
|
struct B2_mutex { |
1214 |
|
int dummy; |
1215 |
|
}; |
1232 |
|
delete mutex; |
1233 |
|
} |
1234 |
|
|
1235 |
+ |
#endif |
1236 |
+ |
|
1237 |
|
|
1238 |
|
/* |
1239 |
|
* Trigger signal USR2 from another thread |
1240 |
|
*/ |
1241 |
|
|
1242 |
+ |
#if !EMULATED_PPC || ASYNC_IRQ |
1243 |
|
void TriggerInterrupt(void) |
1244 |
|
{ |
1198 |
– |
#if EMULATED_PPC |
1199 |
– |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
1200 |
– |
#else |
1201 |
– |
#if 0 |
1202 |
– |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
1203 |
– |
#else |
1245 |
|
if (ready_for_signals) |
1246 |
|
pthread_kill(emul_thread, SIGUSR2); |
1206 |
– |
#endif |
1207 |
– |
#endif |
1247 |
|
} |
1248 |
+ |
#endif |
1249 |
|
|
1250 |
|
|
1251 |
|
/* |
1285 |
|
} |
1286 |
|
|
1287 |
|
|
1248 |
– |
#if !EMULATED_PPC |
1288 |
|
/* |
1289 |
|
* USR2 handler |
1290 |
|
*/ |
1291 |
|
|
1292 |
+ |
#if EMULATED_PPC |
1293 |
+ |
static void sigusr2_handler(int sig) |
1294 |
+ |
{ |
1295 |
+ |
#if ASYNC_IRQ |
1296 |
+ |
extern void HandleInterrupt(void); |
1297 |
+ |
HandleInterrupt(); |
1298 |
+ |
#endif |
1299 |
+ |
} |
1300 |
+ |
#else |
1301 |
|
static void sigusr2_handler(int sig, sigcontext_struct *sc) |
1302 |
|
{ |
1303 |
|
pt_regs *r = sc->regs; |
1379 |
|
} |
1380 |
|
break; |
1381 |
|
#endif |
1334 |
– |
|
1382 |
|
} |
1383 |
|
} |
1384 |
+ |
#endif |
1385 |
|
|
1386 |
|
|
1387 |
|
/* |
1388 |
|
* SIGSEGV handler |
1389 |
|
*/ |
1390 |
|
|
1391 |
+ |
#if !EMULATED_PPC |
1392 |
|
static void sigsegv_handler(int sig, sigcontext_struct *sc) |
1393 |
|
{ |
1394 |
|
pt_regs *r = sc->regs; |
1395 |
+ |
|
1396 |
+ |
// Get effective address |
1397 |
+ |
uint32 addr = r->dar; |
1398 |
+ |
|
1399 |
+ |
#if ENABLE_VOSF |
1400 |
+ |
// Handle screen fault. |
1401 |
+ |
extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction); |
1402 |
+ |
if (Screen_fault_handler((sigsegv_address_t)addr, (sigsegv_address_t)r->nip)) |
1403 |
+ |
return; |
1404 |
+ |
#endif |
1405 |
+ |
|
1406 |
|
num_segv++; |
1407 |
|
|
1408 |
|
// Fault in Mac ROM or RAM? |
1409 |
|
bool mac_fault = (r->nip >= ROM_BASE) && (r->nip < (ROM_BASE + ROM_AREA_SIZE)) || (r->nip >= RAMBase) && (r->nip < (RAMBase + RAMSize)); |
1410 |
|
if (mac_fault) { |
1411 |
|
|
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 |
– |
|
1412 |
|
// "VM settings" during MacOS 8 installation |
1413 |
|
if (r->nip == ROM_BASE + 0x488160 && r->gpr[20] == 0xf8000000) { |
1414 |
|
r->nip += 4; |
1436 |
|
return; |
1437 |
|
} |
1438 |
|
|
1439 |
+ |
// Get opcode and divide into fields |
1440 |
+ |
uint32 opcode = *((uint32 *)r->nip); |
1441 |
+ |
uint32 primop = opcode >> 26; |
1442 |
+ |
uint32 exop = (opcode >> 1) & 0x3ff; |
1443 |
+ |
uint32 ra = (opcode >> 16) & 0x1f; |
1444 |
+ |
uint32 rb = (opcode >> 11) & 0x1f; |
1445 |
+ |
uint32 rd = (opcode >> 21) & 0x1f; |
1446 |
+ |
int32 imm = (int16)(opcode & 0xffff); |
1447 |
+ |
|
1448 |
|
// Analyze opcode |
1449 |
|
enum { |
1450 |
|
TYPE_UNKNOWN, |
1528 |
|
transfer_type = TYPE_STORE; transfer_size = SIZE_HALFWORD; addr_mode = MODE_U; break; |
1529 |
|
} |
1530 |
|
|
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 |
– |
|
1531 |
|
// Ignore ROM writes |
1532 |
|
if (transfer_type == TYPE_STORE && addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) { |
1533 |
|
// 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)); |