home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / os / bsdss4.tz / bsdss4 / bsdss / server / uxkern / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-25  |  7.2 KB  |  346 lines

  1. /* 
  2.  * Mach Operating System
  3.  * Copyright (c) 1992 Carnegie Mellon University
  4.  * All Rights Reserved.
  5.  * 
  6.  * Permission to use, copy, modify and distribute this software and its
  7.  * documentation is hereby granted, provided that both the copyright
  8.  * notice and this permission notice appear in all copies of the
  9.  * software, derivative works or modified versions, and any portions
  10.  * thereof, and that both notices appear in supporting documentation.
  11.  * 
  12.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15.  * 
  16.  * Carnegie Mellon requests users of this software to return to
  17.  * 
  18.  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  19.  *  School of Computer Science
  20.  *  Carnegie Mellon University
  21.  *  Pittsburgh PA 15213-3890
  22.  * 
  23.  * any improvements or extensions that they make and grant Carnegie Mellon 
  24.  * the rights to redistribute these changes.
  25.  */
  26. /*
  27.  * HISTORY
  28.  * $Log:    misc.c,v $
  29.  * Revision 2.3  92/05/25  14:46:29  rwd
  30.  *     Added syscalltrace().
  31.  *     [92/05/25            rwd]
  32.  * 
  33.  * Revision 2.2  92/04/22  14:01:15  rwd
  34.  *     Remove some obsolete code.  Fix includes.
  35.  *     [92/04/22            rwd]
  36.  * 
  37.  * Revision 2.1  92/04/21  17:10:55  rwd
  38.  * BSDSS
  39.  * 
  40.  *
  41.  */
  42. #include <map_time.h>
  43. #include <second_server.h>
  44.  
  45. #include <sys/param.h>
  46. #include <sys/types.h>
  47. #include <sys/buf.h>
  48. #include <sys/mount.h>
  49. #include <ufs/fs.h>
  50. #include <sys/time.h>
  51. #include <sys/reboot.h>
  52.  
  53. #include <uxkern/device.h>
  54. #include <uxkern/device_utils.h>
  55. #include <uxkern/import_mach.h>
  56. #include <uxkern/syscalltrace.h>
  57.  
  58. extern mach_port_t    privileged_host_port;
  59. extern mach_port_t    default_processor_set;
  60.  
  61. #if    SECOND_SERVER
  62. extern int    second_server;
  63. #endif    /* SECOND_SERVER */
  64.  
  65. swapon(){
  66.     printf("swapon called");
  67. }
  68.  
  69. /*
  70.  * New kernel interfaces.
  71.  */
  72.  
  73. #if MAP_TIME
  74. time_value_t *mtime = NULL;
  75.  
  76. init_mapped_time()
  77. {
  78.     kern_return_t rc;
  79.     mach_port_t device_port, pager = MACH_PORT_NULL;
  80.  
  81.     rc = device_open(device_server_port,0,"time",&device_port);
  82.     if (rc != D_SUCCESS) panic("unable to open device time");
  83.  
  84.     rc = device_map(device_port, VM_PROT_READ,
  85.             0, sizeof(time_value_t), &pager, 0);
  86.     if (rc != D_SUCCESS) panic("unable to map device time");
  87.     if (pager == MACH_PORT_NULL) panic("unable to map device time");
  88.     
  89.     rc = vm_map(mach_task_self(), &mtime, sizeof(time_value_t), 0, TRUE,
  90.             pager, 0, 0, VM_PROT_READ, 
  91.             VM_PROT_READ, VM_INHERIT_NONE);
  92.     if (rc != D_SUCCESS) panic("unable to vm_map device time");
  93.  
  94.     rc = mach_port_deallocate(mach_task_self(), pager);
  95.     if (rc != KERN_SUCCESS) panic("unable to deallocate pager");
  96. }
  97.  
  98. microtime(tvp)
  99.     struct timeval *tvp;
  100. {
  101.     *tvp = *(struct timeval *)mtime;
  102. }
  103.  
  104. #else MAP_TIME
  105. init_mapped_time(){}
  106.  
  107. get_time(tvp)
  108.     struct timeval *tvp;
  109. {
  110.     time_value_t    time_value;
  111.  
  112.     kern_timestamp(&time_value);
  113.     tvp->tv_sec = time_value.seconds;
  114.     tvp->tv_usec = time_value.microseconds;
  115. }
  116.  
  117. microtime(tvp)
  118.     struct timeval *tvp;
  119. {
  120.     get_time(tvp);
  121. }
  122.  
  123. #endif MAP_TIME
  124.  
  125. set_time(tvp)
  126.     struct timeval *tvp;
  127. {
  128.     time_value_t    time_value;
  129.  
  130.     time_value.seconds = tvp->tv_sec;
  131.     time_value.microseconds = tvp->tv_usec;
  132.  
  133.     (void) host_set_time(privileged_host_port, time_value);
  134. }
  135.  
  136. int    waittime = -1;
  137.  
  138. Debugger()
  139. {
  140. #if    SECOND_SERVER
  141.     if (second_server) {
  142.         printf("Debugger\n");
  143. #ifdef    i386
  144.         asm("int3");
  145. #else    i386
  146.         task_suspend(mach_task_self());
  147. #endif    i386
  148.         return;
  149.     }
  150. #endif    /* SECOND_SERVER */
  151.  
  152.     boot(0, RB_NOSYNC | RB_DEBUGGER);
  153.  
  154. }
  155.  
  156. boot(paniced, flags)
  157.     int    paniced;
  158.     int    flags;
  159. {
  160. #if 0
  161.     if (((flags & RB_NOSYNC) == 0) &&
  162.         (waittime < 0) &&
  163.         (bfreelist[0].b_forw != 0) &&
  164.         (mount[0].m_bufp != 0)) {
  165.         /*
  166.          * Force an accurate time into the root file system superblock.
  167.          */
  168.         mount[0].m_bufp->b_un.b_fs->fs_fmod = 1;
  169.  
  170.         waittime = 0;
  171.         (void) splnet();
  172.         printf("syncing disks... ");
  173.  
  174.         update();
  175.  
  176.         {
  177.         register struct buf *bp;
  178.         int    iter, nbusy, obusy;
  179.  
  180.         obusy = 0;
  181.  
  182.         for (iter = 0; iter < 20; iter++) {
  183.             nbusy = 0;
  184.             for (bp = &buf[nbuf]; --bp >= buf; )
  185.             if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY)
  186.                 nbusy++;
  187.             if (nbusy == 0)
  188.             break;
  189.             printf("%d ", nbusy);
  190.  
  191.             if (nbusy != obusy)
  192.             iter = 0;
  193.             obusy = nbusy;
  194.  
  195.             DELAY(40000 * iter);
  196.         }
  197.         }
  198.         printf("done\n");
  199.  
  200.         /*
  201.          * resettodr()?
  202.          */
  203.     }
  204. #else    0
  205.     printf("WARNING: No update\n");
  206. #endif    0
  207.  
  208. #if    SECOND_SERVER
  209.     if (second_server) {
  210.         second__exit();
  211.         return;
  212.     }
  213. #endif    /* SECOND_SERVER */
  214.     (void) host_reboot(privileged_host_port, flags);
  215. }
  216.  
  217. thread_read_times(thread, utv, stv)
  218.     thread_t    thread;
  219.     time_value_t    *utv;
  220.     time_value_t    *stv;
  221. {
  222.     struct thread_basic_info    bi;
  223.     unsigned int            bi_count;
  224.  
  225.     bi_count = THREAD_BASIC_INFO_COUNT;
  226.     (void) thread_info(thread,
  227.                THREAD_BASIC_INFO,
  228.                (thread_info_t)&bi,
  229.                &bi_count);
  230.  
  231.     *utv = bi.user_time;
  232.     *stv = bi.system_time;
  233. }
  234.  
  235. /*
  236.  *    Priorities run from 0 (high) to 31 (low).
  237.  *    The user base priority is 12.
  238.  *    priority = 12 + nice / 2.
  239.  */
  240.  
  241. set_thread_priority(thread, pri)
  242.     thread_t    thread;
  243.     int        pri;
  244. {
  245.     (void) thread_max_priority(thread, default_processor_set, pri);
  246.     (void) thread_priority(thread, pri, FALSE);
  247. }
  248.  
  249. #if MAP_UAREA
  250. #include <sys/user.h>
  251. #include <kern/parallel.h>
  252. #define BACKOFF_SECS 5
  253. #define SHARED_PRIORITY PSPECL
  254.  
  255. extern int hz;
  256.  
  257. boolean_t share_try_lock(p, lock)
  258.     register struct proc *p;
  259.     register struct shared_lock *lock;
  260. {
  261.     if (spin_try_lock(&lock->lock)) {
  262.         if (p->p_shared_rw->us_inuse)
  263.             lock->who = (int)p | KERNEL_USER;
  264.         u.uu_share_lock_count++;
  265.         return TRUE;
  266.     } else
  267.         return FALSE;
  268. }
  269.  
  270. void share_lock(x, p)
  271. register struct shared_lock *x;
  272. register struct proc *p;
  273. {
  274.     if (p->p_shared_rw->us_inuse) {
  275.       while (!spin_try_lock(&(x)->lock) && !share_lock_solid((x)));
  276.       (x)->who = (int)(p) | KERNEL_USER;
  277.     } else {
  278.       spin_lock(&(x)->lock);
  279.     }
  280.     u.uu_share_lock_count++;
  281. }
  282.  
  283. void share_unlock(x, p)
  284. register struct shared_lock *x;
  285. register struct proc *p;
  286. {
  287.     if (p->p_shared_rw->us_inuse) {
  288.       (x)->who = 0;
  289.       spin_unlock(&(x)->lock);
  290.       if ((x)->need_wakeup) {
  291.     unix_master();
  292.     wakeup(x);
  293.     unix_release();
  294.       }
  295.     } else {
  296.       spin_unlock(&(x)->lock);
  297.     }
  298.     if (u.uu_share_lock_count-- == 0) {
  299.     panic("share_unlock < 0");
  300.     u.uu_share_lock_count = 0;
  301.     }
  302. }
  303.  
  304. int share_lock_solid(x)
  305.     register struct shared_lock *x;
  306. {
  307.     unix_master();
  308.     x->need_wakeup++;
  309.     if (spin_try_lock(&x->lock)) {
  310.         x->need_wakeup--;
  311.         unix_release();
  312.         return 1;
  313.     }
  314. /*    printf("[%x]Share_lock_solid %x\n",(int)x, x->who);*/
  315.     if (tsleep(x, SHARED_PRIORITY,"share lock solid", BACKOFF_SECS * hz) ==
  316.                                  EWOULDBLOCK) {
  317.         if ((x->who & ~KERNEL_USER >= (int)proc) &&
  318.             (x->who & ~KERNEL_USER <= (int)procNPROC)) {
  319.             printf("[%x]Unable to get lock from %x\n",(int)x, x->who);
  320.             panic("share_lock_solid");
  321.         } else {
  322.             printf("[%x]Taking scribbled share_lock\n",(int)x);
  323.             share_lock_init(x);
  324.             x->need_wakeup++;
  325.         }
  326.     }
  327.     (x)->need_wakeup--; /* This protected by the master lock */
  328. /*    printf("[%x]Share_lock_solid\n",(int)x);*/
  329.     unix_release();
  330.     return 0;
  331. }
  332.  
  333. #endif MAP_UAREA
  334.  
  335. sysctrace(p, uap, retval)
  336.     register struct proc *p;
  337.     register struct args {
  338.     int pid;
  339.     } *uap;
  340.     int *retval;
  341. {
  342. #if SYSCALLTRCAE
  343.     syscalltrace = uap->pid;
  344. #endif
  345. }
  346.