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.9 by gbeauche, 2003-10-12T05:44:14Z vs.
Revision 1.19 by gbeauche, 2003-12-05T12:41:19Z

# Line 109 | Line 109
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"
# Line 145 | Line 146
146   const char ROM_FILE_NAME[] = "ROM";
147   const char ROM_FILE_NAME2[] = "Mac OS ROM";
148  
149 < const uint32 RAM_BASE = 0x20000000;                     // Base address of RAM
149 > const uintptr RAM_BASE = 0x20000000;            // Base address of RAM
150   const uint32 SIG_STACK_SIZE = 0x10000;          // Size of signal stack
151  
152  
# Line 172 | Line 173 | void *TOC;                             // Small data pointer (r13
173   #endif
174   uint32 RAMBase;                 // Base address of Mac RAM
175   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
176   uint32 KernelDataAddr;  // Address of Kernel Data
177   uint32 BootGlobsAddr;   // Address of BootGlobs structure at top of Mac RAM
178   uint32 PVR;                             // Theoretical PVR
# Line 183 | Line 181 | int64 BusClockSpeed;   // Bus clock speed
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
190 static bool sheep_area_mapped = false;          // Flag: SheepShaver data area mmap()ed
188   static bool lm_area_mapped = false;                     // Flag: Low Memory area mmap()ped
189   static int kernel_area = -1;                            // SHM ID of Kernel Data area
190   static bool rom_area_mapped = false;            // Flag: Mac ROM mmap()ped
# Line 216 | Line 213 | static bool emul_thread_fatal = false;
213   static sigregs sigsegv_regs;                            // Register dump when crashed
214   #endif
215  
216 + uintptr SheepMem::zero_page = 0;                        // Address of ro page filled in with zeros
217 + uintptr SheepMem::base = 0x60000000;            // Address of SheepShaver data
218 + uintptr SheepMem::top = 0;                                      // Top of SheepShaver data (stack like storage)
219 +
220  
221   // Prototypes
222   static void Quit(void);
# Line 224 | Line 225 | static void *nvram_func(void *arg);
225   static void *tick_func(void *arg);
226   #if EMULATED_PPC
227   static void sigusr2_handler(int sig);
228 + extern void emul_ppc(uint32 start);
229 + extern void init_emul_ppc(void);
230 + extern void exit_emul_ppc(void);
231   #else
232   static void sigusr2_handler(int sig, sigcontext_struct *sc);
233   static void sigsegv_handler(int sig, sigcontext_struct *sc);
# Line 232 | Line 236 | static void sigill_handler(int sig, sigc
236  
237  
238   // From asm_linux.S
239 < #if EMULATED_PPC
236 < extern int atomic_add(int *var, int v);
237 < extern int atomic_and(int *var, int v);
238 < extern int atomic_or(int *var, int v);
239 < #else
239 > #if !EMULATED_PPC
240   extern "C" void *get_toc(void);
241   extern "C" void *get_sp(void);
242   extern "C" void flush_icache_range(void *start, void *end);
# Line 251 | Line 251 | extern void paranoia_check(void);
251   #endif
252  
253  
254 + #if EMULATED_PPC
255 + /*
256 + *  Atomic operations
257 + */
258 +
259 + #if HAVE_SPINLOCKS
260 + static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED;
261 + #else
262 + #define spin_lock(LOCK)
263 + #define spin_unlock(LOCK)
264 + #endif
265 +
266 + int atomic_add(int *var, int v)
267 + {
268 +        spin_lock(&atomic_ops_lock);
269 +        int ret = *var;
270 +        *var += v;
271 +        spin_unlock(&atomic_ops_lock);
272 +        return ret;
273 + }
274 +
275 + int atomic_and(int *var, int v)
276 + {
277 +        spin_lock(&atomic_ops_lock);
278 +        int ret = *var;
279 +        *var &= v;
280 +        spin_unlock(&atomic_ops_lock);
281 +        return ret;
282 + }
283 +
284 + int atomic_or(int *var, int v)
285 + {
286 +        spin_lock(&atomic_ops_lock);
287 +        int ret = *var;
288 +        *var |= v;
289 +        spin_unlock(&atomic_ops_lock);
290 +        return ret;
291 + }
292 + #endif
293 +
294 +
295   /*
296   *  Main program
297   */
# Line 269 | Line 310 | int main(int argc, char **argv)
310          char str[256];
311          uint32 *boot_globs;
312          int16 i16;
272        int drive, driver;
313          int rom_fd;
314          FILE *proc_file;
315          const char *rom_path;
# Line 431 | Line 471 | int main(int argc, char **argv)
471                  ErrorAlert(str);
472                  goto quit;
473          }
474 <        kernel_data = (KernelData *)0x68ffe000;
474 >        kernel_data = (KernelData *)KERNEL_DATA_BASE;
475          emulator_data = &kernel_data->ed;
476 <        KernelDataAddr = (uint32)kernel_data;
476 >        KernelDataAddr = KERNEL_DATA_BASE;
477          D(bug("Kernel Data at %p, Emulator Data at %p\n", kernel_data, emulator_data));
478  
479          // Create area for SheepShaver data
480 <        if (vm_acquire_fixed((char *)SHEEP_BASE, SHEEP_SIZE) < 0) {
480 >        if (!SheepMem::Init()) {
481                  sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno));
482                  ErrorAlert(str);
483                  goto quit;
484          }
445        SheepStack1Base = SHEEP_BASE + 0x10000;
446        SheepStack2Base = SheepStack1Base + 0x10000;
447        SheepThunksBase = SheepStack2Base + 0x1000;
448        sheep_area_mapped = true;
485  
486          // Create area for Mac ROM
487          if (vm_acquire_fixed((char *)ROM_BASE, ROM_AREA_SIZE) < 0) {
# Line 524 | Line 560 | int main(int argc, char **argv)
560          XPRAMInit();
561  
562          // Set boot volume
563 <        drive = PrefsFindInt32("bootdrive");
563 >        i16 = PrefsFindInt32("bootdrive");
564          XPRAM[0x1378] = i16 >> 8;
565          XPRAM[0x1379] = i16 & 0xff;
566 <        driver = PrefsFindInt32("bootdriver");
566 >        i16 = PrefsFindInt32("bootdriver");
567          XPRAM[0x137a] = i16 >> 8;
568          XPRAM[0x137b] = i16 & 0xff;
569  
# Line 540 | Line 576 | int main(int argc, char **argv)
576          boot_globs[1] = htonl(RAMSize);
577          boot_globs[2] = htonl((uint32)-1);                      // End of bank table
578  
579 +        // Init thunks
580 +        if (!ThunksInit())
581 +                goto quit;
582 +
583          // Init drivers
584          SonyInit();
585          DiskInit();
# Line 583 | Line 623 | int main(int argc, char **argv)
623          // Initialize Kernel Data
624          memset(kernel_data, 0, sizeof(KernelData));
625          if (ROMType == ROMTYPE_NEWWORLD) {
626 <                static uint32 of_dev_tree[4] = {0, 0, 0, 0};
627 <                static uint8 vector_lookup_tbl[128];
628 <                static uint8 vector_mask_tbl[64];
626 >                uintptr of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32));
627 >                memset((void *)of_dev_tree, 0, 4 * sizeof(uint32));
628 >                uintptr vector_lookup_tbl = SheepMem::Reserve(128);
629 >                uintptr vector_mask_tbl = SheepMem::Reserve(64);
630                  memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80);
631 <                memset(vector_lookup_tbl, 0, 128);
632 <                memset(vector_mask_tbl, 0, 64);
631 >                memset((void *)vector_lookup_tbl, 0, 128);
632 >                memset((void *)vector_mask_tbl, 0, 64);
633                  kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE);
634 <                kernel_data->v[0xb84 >> 2] = htonl((uint32)of_dev_tree);        // OF device tree base
635 <                kernel_data->v[0xb90 >> 2] = htonl((uint32)vector_lookup_tbl);
636 <                kernel_data->v[0xb94 >> 2] = htonl((uint32)vector_mask_tbl);
634 >                kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree);                        // OF device tree base
635 >                kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl);
636 >                kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl);
637                  kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE);                           // OpenPIC base
638                  kernel_data->v[0xbb0 >> 2] = htonl(0);                                          // ADB base
639                  kernel_data->v[0xc20 >> 2] = htonl(RAMSize);
# Line 628 | Line 669 | int main(int argc, char **argv)
669          D(bug("Initializing Low Memory...\n"));
670          memset(NULL, 0, 0x3000);
671          WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h'));                  // Signature to detect SheepShaver
672 <        WriteMacInt32(XLM_KERNEL_DATA, (uint32)kernel_data);                    // For trap replacement routines
672 >        WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr);                                 // For trap replacement routines
673          WriteMacInt32(XLM_PVR, PVR);                                                                    // Theoretical PVR
674          WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed);                                    // For DriverServicesLib patch
675          WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN);                // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode)
676 < #if EMULATED_PPC
677 <        WriteMacInt32(XLM_ETHER_INIT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_INIT));
678 <        WriteMacInt32(XLM_ETHER_TERM, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_TERM));
638 <        WriteMacInt32(XLM_ETHER_OPEN, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_OPEN));
639 <        WriteMacInt32(XLM_ETHER_CLOSE, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_CLOSE));
640 <        WriteMacInt32(XLM_ETHER_WPUT, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_WPUT));
641 <        WriteMacInt32(XLM_ETHER_RSRV, POWERPC_NATIVE_OP_FUNC(NATIVE_ETHER_RSRV));
642 <        WriteMacInt32(XLM_VIDEO_DOIO, POWERPC_NATIVE_OP_FUNC(NATIVE_VIDEO_DO_DRIVER_IO));
643 < #else
644 <        WriteMacInt32(XLM_TOC, (uint32)TOC);                                                    // TOC pointer of emulator
645 <        WriteMacInt32(XLM_ETHER_INIT, (uint32)InitStreamModule);                // DLPI ethernet driver functions
646 <        WriteMacInt32(XLM_ETHER_TERM, (uint32)TerminateStreamModule);
647 <        WriteMacInt32(XLM_ETHER_OPEN, (uint32)ether_open);
648 <        WriteMacInt32(XLM_ETHER_CLOSE, (uint32)ether_close);
649 <        WriteMacInt32(XLM_ETHER_WPUT, (uint32)ether_wput);
650 <        WriteMacInt32(XLM_ETHER_RSRV, (uint32)ether_rsrv);
651 <        WriteMacInt32(XLM_VIDEO_DOIO, (uint32)VideoDoDriverIO);
676 >        WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage());                             // Pointer to read-only page with all bits set to 0
677 > #if !EMULATED_PPC
678 >        WriteMacInt32(XLM_TOC, (uint32)TOC);                                                            // TOC pointer of emulator
679   #endif
680 +        WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT));       // DLPI ethernet driver functions
681 +        WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM));
682 +        WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN));
683 +        WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE));
684 +        WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT));
685 +        WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV));
686 +        WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO));
687          D(bug("Low Memory initialized\n"));
688  
689          // Start 60Hz thread
# Line 743 | Line 777 | quit:
777  
778   static void Quit(void)
779   {
780 + #if EMULATED_PPC
781 +        // Exit PowerPC emulation
782 +        exit_emul_ppc();
783 + #endif
784 +
785          // Stop 60Hz thread
786          if (tick_thread_active) {
787                  pthread_cancel(tick_thread);
# Line 799 | Line 838 | static void Quit(void)
838          DiskExit();
839          SonyExit();
840  
841 +        // Delete SheepShaver globals
842 +        SheepMem::Exit();
843 +
844          // Delete RAM area
845          if (ram_area_mapped)
846                  vm_release((char *)RAM_BASE, RAMSize);
# Line 846 | Line 888 | static void Quit(void)
888   */
889  
890   #if EMULATED_PPC
849 extern void emul_ppc(uint32 start);
850 extern void init_emul_ppc(void);
891   void jump_to_rom(uint32 entry)
892   {
893          init_emul_ppc();
# Line 912 | Line 952 | void Execute68kTrap(uint16 trap, M68kReg
952          uint16 proc[2] = {trap, M68K_RTS};
953          Execute68k((uint32)proc, r);
954   }
915
916
917 /*
918 *  Execute PPC code from EMUL_OP routine (real mode switch)
919 */
920
921 void ExecutePPC(void (*func)())
922 {
923        uint32 tvect[2] = {(uint32)func, 0};    // Fake TVECT
924        RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect);
925        M68kRegisters r;
926        Execute68k((uint32)&desc, &r);
927 }
955   #endif
956  
957  
# Line 1003 | Line 1030 | void MakeExecutable(int dummy, void *sta
1030  
1031   void PatchAfterStartup(void)
1032   {
1006 #if EMULATED_PPC
1033          ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL);
1008 #else
1009        ExecutePPC(VideoInstallAccel);
1010 #endif
1034          InstallExtFS();
1035   }
1036  
# Line 1110 | Line 1133 | static void *tick_func(void *arg)
1133  
1134   void Set_pthread_attr(pthread_attr_t *attr, int priority)
1135   {
1136 <        // nothing to do
1136 > #ifdef HAVE_PTHREADS
1137 >        pthread_attr_init(attr);
1138 > #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
1139 >        // Some of these only work for superuser
1140 >        if (geteuid() == 0) {
1141 >                pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
1142 >                pthread_attr_setschedpolicy(attr, SCHED_FIFO);
1143 >                struct sched_param fifo_param;
1144 >                fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) +
1145 >                                              sched_get_priority_max(SCHED_FIFO)) / 2 +
1146 >                                             priority);
1147 >                pthread_attr_setschedparam(attr, &fifo_param);
1148 >        }
1149 >        if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) {
1150 > #ifdef PTHREAD_SCOPE_BOUND_NP
1151 >            // If system scope is not available (eg. we're not running
1152 >            // with CAP_SCHED_MGT capability on an SGI box), try bound
1153 >            // scope.  It exposes pthread scheduling to the kernel,
1154 >            // without setting realtime priority.
1155 >            pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP);
1156 > #endif
1157 >        }
1158 > #endif
1159 > #endif
1160   }
1161  
1162  
# Line 1325 | Line 1371 | static void sigusr2_handler(int sig, sig
1371                                          if (InterruptFlags & INTFLAG_VIA) {
1372                                                  ClearInterruptFlag(INTFLAG_VIA);
1373                                                  ADBInterrupt();
1374 <                                                ExecutePPC(VideoVBL);
1374 >                                                ExecuteNative(NATIVE_VIDEO_VBL);
1375                                          }
1376                                  }
1377   #endif
# Line 1722 | Line 1768 | rti:;
1768  
1769  
1770   /*
1771 + *  Helpers to share 32-bit addressable data with MacOS
1772 + */
1773 +
1774 + bool SheepMem::Init(void)
1775 + {
1776 +        if (vm_acquire_fixed((char *)base, size) < 0)
1777 +                return false;
1778 +
1779 +        zero_page = base + size;
1780 +
1781 +        int page_size = getpagesize();
1782 +        if (vm_acquire_fixed((char *)zero_page, page_size) < 0)
1783 +                return false;
1784 +        memset((char *)zero_page, 0, page_size);
1785 +        if (vm_protect((char *)zero_page, page_size, VM_PAGE_READ) < 0)
1786 +                return false;
1787 +
1788 +        top = base + size;
1789 +        return true;
1790 + }
1791 +
1792 + void SheepMem::Exit(void)
1793 + {
1794 +        if (top) {
1795 +                // The zero page is next to SheepShaver globals
1796 +                vm_release((void *)base, size + getpagesize());
1797 +        }
1798 + }
1799 +
1800 +
1801 + /*
1802   *  Display alert
1803   */
1804  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines