Back to index

lightning-sunbird  0.9+nobinonly
nextstep.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "primpl.h"
00039 
00040 #import <mach/mach.h>
00041 #import <mach/mach_error.h>
00042 #import <mach-o/dyld.h>
00043 #include <stdlib.h>
00044 #include <string.h>
00045 #include <sys/types.h>
00046 #include <sys/mman.h>
00047 #include <syscall.h>
00048 
00049 
00050 
00051 /*     These functions are hidden in NEXTSTEP, but beacuse they have syscall()
00052 **     entries I can wrap these into their corresponding missing function.
00053 */
00054 caddr_t
00055 mmap(caddr_t addr, size_t len, int prot, int flags,
00056           int fildes, off_t off)
00057 {
00058        return (caddr_t) syscall (SYS_mmap, addr, len, prot, flags, fildes, off);
00059 }
00060 
00061 int
00062 munmap(caddr_t addr, size_t len)
00063 {
00064        return syscall (SYS_munmap, addr, len);
00065 }
00066 
00067 int
00068 mprotect(caddr_t addr, size_t len, int prot)
00069 {
00070        return syscall (SYS_mprotect, addr, len, prot);
00071 }
00072 
00073 
00074 /* If found the brk() symbol in the sahred libraries but no syscall() entry ...
00075 ** I don't know whether it will work ...
00076 int brk(void *endds)
00077 {
00078        return syscall ();
00079 }
00080 */
00081 
00082 void *sbrk(int incr)
00083 {
00084        return (void *) syscall (SYS_sbrk, incr);
00085 }
00086 
00087 /*     These are my mach based versions, untested and probably bad ...
00088 */
00089 caddr_t my_mmap(caddr_t addr, size_t len, int prot, int flags,
00090           int fildes, off_t off)
00091 {
00092        kern_return_t ret_val;
00093        
00094        /*     First map ...
00095        */
00096        ret_val = map_fd ( fildes,                              /* fd                       */
00097                          (vm_offset_t) off,             /* offset                   */
00098                                      (vm_offset_t*)&addr,             /* address                  */
00099                                      TRUE,                                   /* find_space        */
00100                                      (vm_size_t) len);                /* size                            */
00101 
00102        if (ret_val != KERN_SUCCESS) {
00103        mach_error("Error calling map_fd() in mmap", ret_val );
00104               return (caddr_t)0;
00105        }
00106        
00107        /*     ... then protect (this is probably bad)
00108        */
00109        ret_val = vm_protect( task_self(),               /* target_task              */
00110                                            (vm_address_t)addr, /* address                  */
00111                                            (vm_size_t) len,           /* size                     */
00112                                            FALSE,                                   /* set_maximum              */
00113                                            (vm_prot_t) prot);         /* new_protection    */
00114        if (ret_val != KERN_SUCCESS) {
00115               mach_error("vm_protect in mmap()", ret_val );
00116               return (caddr_t)0;
00117        }
00118        
00119        return addr;
00120 }
00121 
00122 int my_munmap(caddr_t addr, size_t len)
00123 {
00124        kern_return_t ret_val;
00125 
00126        ret_val = vm_deallocate(task_self(),
00127                                                  (vm_address_t) addr,
00128                                                  (vm_size_t) len);
00129 
00130        if (ret_val != KERN_SUCCESS) {
00131               mach_error("vm_deallocate in munmap()", ret_val);
00132               return -1;
00133        }
00134        
00135        return 0;
00136 }
00137 
00138 int my_mprotect(caddr_t addr, size_t len, int prot)
00139 {
00140        vm_prot_t mach_prot;
00141        kern_return_t ret_val;
00142        
00143        switch (prot) {
00144               case PROT_READ:             mach_prot = VM_PROT_READ;          break;
00145               case PROT_WRITE:     mach_prot = VM_PROT_WRITE;         break;
00146               case PROT_EXEC:             mach_prot = VM_PROT_EXECUTE;       break;
00147               case PROT_NONE:             mach_prot = VM_PROT_NONE;          break;
00148        }
00149        
00150        ret_val = vm_protect(task_self(),                /* target_task              */
00151                                            (vm_address_t)addr, /* address                  */
00152                                            (vm_size_t) len,           /* size                     */
00153                                            FALSE,                                   /* set_maximum              */
00154                                            (vm_prot_t) prot);         /* new_protection    */
00155 
00156        if (ret_val != KERN_SUCCESS) {
00157               mach_error("vm_protect in mprotect()", ret_val);
00158               return -1;
00159        }
00160        
00161        return 0;
00162 }
00163 
00164 char *strdup(const char *s1)
00165 {
00166        int len = strlen (s1);
00167        char *copy = (char*) malloc (len+1);
00168        
00169        if (copy == (char*)0)
00170               return (char*)0;
00171 
00172        strcpy (copy, s1);
00173 
00174        return copy;
00175 }
00176 
00177 /* Stub rld functions
00178 */
00179 extern NSObjectFileImageReturnCode NSCreateObjectFileImageFromFile(
00180     const char *pathName,
00181     NSObjectFileImage *objectFileImage)
00182 {
00183        return NSObjectFileImageFailure;
00184 }
00185 
00186 extern void * NSAddressOfSymbol(
00187     NSSymbol symbol)
00188 {
00189        return NULL;
00190 }
00191 
00192 extern NSModule NSLinkModule(
00193     NSObjectFileImage objectFileImage, 
00194     const char *moduleName, /* can be NULL */
00195     enum bool bindNow)
00196 {
00197        return NULL;
00198 }
00199 
00200 extern NSSymbol NSLookupAndBindSymbol(
00201     const char *symbolName)
00202 {
00203        return NULL;
00204 }
00205 
00206 extern enum bool NSUnLinkModule(
00207     NSModule module, 
00208     enum bool keepMemoryMapped)
00209 {
00210        return 0;
00211 }
00212 
00213 
00214 
00215 void _MD_EarlyInit(void)
00216 {
00217 }
00218 
00219 PRWord *_MD_HomeGCRegisters(PRThread *t, int isCurrent, int *np)
00220 {
00221 #ifndef _PR_PTHREADS
00222     if (isCurrent) {
00223        (void) sigsetjmp(CONTEXT(t), 1);
00224     }
00225     *np = sizeof(CONTEXT(t)) / sizeof(PRWord);
00226     return (PRWord *) CONTEXT(t);
00227 #else
00228        *np = 0;
00229        return NULL;
00230 #endif
00231 }
00232 
00233 #ifndef _PR_PTHREADS
00234 
00235 void
00236 _MD_SET_PRIORITY(_MDThread *thread, PRUintn newPri)
00237 {
00238     return;
00239 }
00240 
00241 PRStatus
00242 _MD_InitializeThread(PRThread *thread)
00243 {
00244        return PR_SUCCESS;
00245 }
00246 
00247 PRStatus
00248 _MD_WAIT(PRThread *thread, PRIntervalTime ticks)
00249 {
00250     PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
00251     _PR_MD_SWITCH_CONTEXT(thread);
00252     return PR_SUCCESS;
00253 }
00254 
00255 PRStatus
00256 _MD_WAKEUP_WAITER(PRThread *thread)
00257 {
00258     if (thread) {
00259        PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
00260     }
00261     return PR_SUCCESS;
00262 }
00263 
00264 /* These functions should not be called for NEXTSTEP */
00265 void
00266 _MD_YIELD(void)
00267 {
00268     PR_NOT_REACHED("_MD_YIELD should not be called for NEXTSTEP.");
00269 }
00270 
00271 PRStatus
00272 _MD_CREATE_THREAD(
00273     PRThread *thread,
00274     void (*start) (void *),
00275     PRThreadPriority priority,
00276     PRThreadScope scope,
00277     PRThreadState state,
00278     PRUint32 stackSize)
00279 {
00280     PR_NOT_REACHED("_MD_CREATE_THREAD should not be called for NEXTSTEP.");
00281        return PR_FAILURE;
00282 }
00283 
00284 #endif