]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/bsd_kern.c
xnu-2422.100.13.tar.gz
[apple/xnu.git] / osfmk / kern / bsd_kern.c
index 64f02fb12a17e793776b3e677211419940f9a6de..8d4f388ede9eb55cfa2c8d8b0396af8e8a71a74a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 #include <mach/mach_types.h>
+#include <mach/machine/vm_param.h>
+#include <mach/task.h>
 
 #include <kern/kern_types.h>
+#include <kern/ledger.h>
 #include <kern/processor.h>
 #include <kern/thread.h>
 #include <kern/task.h>
 #include <ipc/ipc_port.h>
 #include <ipc/ipc_object.h>
 #include <vm/vm_map.h>
+#include <vm/vm_kern.h>
 #include <vm/pmap.h>
 #include <vm/vm_protos.h> /* last */
+#include <sys/resource.h>
 
 #undef thread_should_halt
-#undef ipc_port_release
 
 /* BSD KERN COMPONENT INTERFACE */
 
@@ -52,13 +56,13 @@ extern unsigned int not_in_kdp; /* Skip acquiring locks if we're in kdp */
 thread_t get_firstthread(task_t);
 int get_task_userstop(task_t);
 int get_thread_userstop(thread_t);
-boolean_t thread_should_abort(thread_t);
 boolean_t current_thread_aborted(void);
 void task_act_iterate_wth_args(task_t, void(*)(thread_t, void *), void *);
-void ipc_port_release(ipc_port_t);
-boolean_t is_thread_active(thread_t);
 kern_return_t get_signalact(task_t , thread_t *, int);
 int get_vmsubmap_entries(vm_map_t, vm_object_offset_t, vm_object_offset_t);
+void syscall_exit_funnelcheck(void);
+int fill_task_rusage_v2(task_t task, struct rusage_info_v2 *ri);
+
 
 /*
  *
@@ -68,6 +72,14 @@ void  *get_bsdtask_info(task_t t)
        return(t->bsd_info);
 }
 
+/*
+ *
+ */
+void *get_bsdthreadtask_info(thread_t th)
+{
+       return(th->task != TASK_NULL ? th->task->bsd_info : NULL);
+}
+
 /*
  *
  */
@@ -84,6 +96,15 @@ void *get_bsdthread_info(thread_t th)
        return(th->uthread);
 }
 
+/*
+ * XXX
+ */
+int get_thread_lock_count(thread_t th);                /* forced forward */
+int get_thread_lock_count(thread_t th)
+{
+       return(th->mutex_count);
+}
+
 /*
  * XXX: wait for BSD to  fix signal code
  * Until then, we cannot block here.  We know the task
@@ -92,7 +113,7 @@ void *get_bsdthread_info(thread_t th)
  */
 thread_t get_firstthread(task_t task)
 {
-       thread_t        thread = (thread_t)queue_first(&task->threads);
+       thread_t        thread = (thread_t)(void *)queue_first(&task->threads);
 
        if (queue_end(&task->threads, (queue_entry_t)thread))
                thread = THREAD_NULL;
@@ -120,17 +141,17 @@ get_signalact(
                return (KERN_FAILURE);
        }
 
-       for (inc  = (thread_t)queue_first(&task->threads);
+       for (inc  = (thread_t)(void *)queue_first(&task->threads);
                        !queue_end(&task->threads, (queue_entry_t)inc); ) {
-                thread_mtx_lock(inc);
-                if (inc->active  && 
-                           (inc->state & (TH_ABORT|TH_ABORT_SAFELY)) != TH_ABORT) {
-                    thread = inc;
-                                       break;
-                }
-                thread_mtx_unlock(inc);
-
-                               inc = (thread_t)queue_next(&inc->task_threads);
+               thread_mtx_lock(inc);
+               if (inc->active &&
+                               (inc->sched_flags & TH_SFLAG_ABORTED_MASK) != TH_SFLAG_ABORT) {
+                       thread = inc;
+                       break;
+               }
+               thread_mtx_unlock(inc);
+
+               inc = (thread_t)(void *)queue_next(&inc->task_threads);
        }
 
        if (result_out) 
@@ -168,13 +189,13 @@ check_actforsig(
                return (KERN_FAILURE);
        }
 
-       for (inc  = (thread_t)queue_first(&task->threads);
+       for (inc  = (thread_t)(void *)queue_first(&task->threads);
                        !queue_end(&task->threads, (queue_entry_t)inc); ) {
                if (inc == thread) {
                        thread_mtx_lock(inc);
 
                        if (inc->active  && 
-                               (inc->state & (TH_ABORT|TH_ABORT_SAFELY)) != TH_ABORT) {
+                                       (inc->sched_flags & TH_SFLAG_ABORTED_MASK) != TH_SFLAG_ABORT) {
                                result = KERN_SUCCESS;
                                break;
                        }
@@ -183,7 +204,7 @@ check_actforsig(
                        break;
                }
 
-               inc = (thread_t)queue_next(&inc->task_threads);
+               inc = (thread_t)(void *)queue_next(&inc->task_threads);
        }
 
        if (result == KERN_SUCCESS) {
@@ -198,6 +219,11 @@ check_actforsig(
        return (result);
 }
 
+ledger_t  get_task_ledger(task_t t)
+{
+       return(t->ledger);
+}
+
 /*
  * This is only safe to call from a thread executing in
  * in the task's context or if the task is locked  Otherwise,
@@ -235,6 +261,22 @@ ipc_space_t  get_task_ipcspace(task_t t)
        return(t->itk_space);
 }
 
+int get_task_numactivethreads(task_t task)
+{
+       thread_t        inc;
+       int num_active_thr=0;
+       task_lock(task);
+
+       for (inc  = (thread_t)(void *)queue_first(&task->threads);
+                       !queue_end(&task->threads, (queue_entry_t)inc); inc = (thread_t)(void *)queue_next(&inc->task_threads)) 
+       {
+               if(inc->active)
+                       num_active_thr++;
+       }
+       task_unlock(task);
+       return num_active_thr;
+}
+
 int  get_task_numacts(task_t t)
 {
        return(t->thread_count);
@@ -243,29 +285,39 @@ int  get_task_numacts(task_t t)
 /* does this machine need  64bit register set for signal handler */
 int is_64signalregset(void)
 {
-       task_t t = current_task();
-       if(t->taskFeatures[0] & tf64BitData)
+       if (task_has_64BitData(current_task())) {
                return(1);
-       else
-               return(0);
+       }
+
+       return(0);
 }
 
 /*
- * The old map reference is returned.
+ * Swap in a new map for the task/thread pair; the old map reference is
+ * returned.
  */
 vm_map_t
-swap_task_map(task_t task,vm_map_t map)
+swap_task_map(task_t task, thread_t thread, vm_map_t map, boolean_t doswitch)
 {
-       thread_t thread = current_thread();
        vm_map_t old_map;
 
        if (task != thread->task)
                panic("swap_task_map");
 
        task_lock(task);
+       mp_disable_preemption();
        old_map = task->map;
        thread->map = task->map = map;
+       if (doswitch) {
+               pmap_switch(map->pmap);
+       }
+       mp_enable_preemption();
        task_unlock(task);
+
+#if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
+       inval_copy_windows(thread);
+#endif
+
        return old_map;
 }
 
@@ -280,53 +332,59 @@ pmap_t  get_task_pmap(task_t t)
 /*
  *
  */
-pmap_t  get_map_pmap(vm_map_t map)
-{
-       return(map->pmap);
-}
-/*
- *
- */
-task_t get_threadtask(thread_t th)
+uint64_t get_task_resident_size(task_t task) 
 {
-       return(th->task);
+       vm_map_t map;
+       
+       map = (task == kernel_task) ? kernel_map: task->map;
+       return((uint64_t)pmap_resident_count(map->pmap) * PAGE_SIZE_64);
 }
 
-
 /*
  *
  */
-boolean_t is_thread_idle(thread_t th)
-{
-       return((th->state & TH_IDLE) == TH_IDLE);
+uint64_t get_task_phys_footprint(task_t task) 
+{      
+       kern_return_t ret;
+       ledger_amount_t credit, debit;
+       
+       ret = ledger_get_entries(task->ledger, task_ledgers.phys_footprint, &credit, &debit);
+       if (KERN_SUCCESS == ret) {
+               return (credit - debit);
+       }
+
+       return 0;
 }
 
 /*
  *
  */
-boolean_t is_thread_running(thread_t th)
-{
-       return((th->state & TH_RUN) == TH_RUN);
+uint64_t get_task_phys_footprint_max(task_t task) 
+{      
+       kern_return_t ret;
+       ledger_amount_t max;
+       
+       ret = ledger_get_maximum(task->ledger, task_ledgers.phys_footprint, &max);
+       if (KERN_SUCCESS == ret) {
+               return max;
+       }
+
+       return 0;
 }
 
 /*
  *
  */
-thread_t
-getshuttle_thread(
-       thread_t        th)
+pmap_t  get_map_pmap(vm_map_t map)
 {
-       return(th);
+       return(map->pmap);
 }
-
 /*
  *
  */
-thread_t
-getact_thread(
-       thread_t        th)
+task_t get_threadtask(thread_t th)
 {
-       return(th);
+       return(th->task);
 }
 
 /*
@@ -439,6 +497,26 @@ get_thread_userstop(
        return(th->user_stop_count);
 }
 
+/*
+ *
+ */
+boolean_t
+get_task_pidsuspended(
+       task_t task)
+{
+    return (task->pidsuspended);
+}
+
+/*
+ *
+ */
+boolean_t 
+get_task_frozen(
+       task_t task)
+{
+    return (task->frozen);   
+}
+
 /*
  *
  */
@@ -446,7 +524,7 @@ boolean_t
 thread_should_abort(
        thread_t th)
 {
-       return ((th->state & (TH_ABORT|TH_ABORT_SAFELY)) == TH_ABORT);
+       return ((th->sched_flags & TH_SFLAG_ABORTED_MASK) == TH_SFLAG_ABORT);
 }
 
 /*
@@ -464,14 +542,14 @@ current_thread_aborted (
        thread_t th = current_thread();
        spl_t s;
 
-       if ((th->state & (TH_ABORT|TH_ABORT_SAFELY)) == TH_ABORT &&
+       if ((th->sched_flags & TH_SFLAG_ABORTED_MASK) == TH_SFLAG_ABORT &&
                        (th->options & TH_OPT_INTMASK) != THREAD_UNINT)
                return (TRUE);
-       if (th->state & TH_ABORT_SAFELY) {
+       if (th->sched_flags & TH_SFLAG_ABORTSAFELY) {
                s = splsched();
                thread_lock(th);
-               if (th->state & TH_ABORT_SAFELY)
-                       th->state &= ~(TH_ABORT|TH_ABORT_SAFELY);
+               if (th->sched_flags & TH_SFLAG_ABORTSAFELY)
+                       th->sched_flags &= ~TH_SFLAG_ABORTED_MASK;
                thread_unlock(th);
                splx(s);
        }
@@ -491,28 +569,15 @@ task_act_iterate_wth_args(
 
        task_lock(task);
 
-       for (inc  = (thread_t)queue_first(&task->threads);
+       for (inc  = (thread_t)(void *)queue_first(&task->threads);
                        !queue_end(&task->threads, (queue_entry_t)inc); ) {
                (void) (*func_callback)(inc, func_arg);
-               inc = (thread_t)queue_next(&inc->task_threads);
+               inc = (thread_t)(void *)queue_next(&inc->task_threads);
        }
 
        task_unlock(task);
 }
 
-void
-ipc_port_release(
-       ipc_port_t port)
-{
-       ipc_object_release(&(port)->ip_object);
-}
-
-boolean_t
-is_thread_active(
-       thread_t th)
-{
-       return(th->active);
-}
 
 void
 astbsd_on(void)
@@ -523,3 +588,214 @@ astbsd_on(void)
        ast_on_fast(AST_BSD);
        (void)ml_set_interrupts_enabled(reenable);
 }
+
+
+#include <sys/bsdtask_info.h>
+
+void
+fill_taskprocinfo(task_t task, struct proc_taskinfo_internal * ptinfo)
+{
+       vm_map_t map;
+       task_absolutetime_info_data_t   tinfo;
+       thread_t thread;
+       uint32_t cswitch = 0, numrunning = 0;
+       uint32_t syscalls_unix = 0;
+       uint32_t syscalls_mach = 0;
+
+       map = (task == kernel_task)? kernel_map: task->map;
+
+       ptinfo->pti_virtual_size  = map->size;
+       ptinfo->pti_resident_size =
+               (mach_vm_size_t)(pmap_resident_count(map->pmap))
+               * PAGE_SIZE_64;
+
+       task_lock(task);
+
+       ptinfo->pti_policy = ((task != kernel_task)?
+                                          POLICY_TIMESHARE: POLICY_RR);
+
+       tinfo.threads_user = tinfo.threads_system = 0;
+       tinfo.total_user = task->total_user_time;
+       tinfo.total_system = task->total_system_time;
+
+       queue_iterate(&task->threads, thread, thread_t, task_threads) {
+               uint64_t    tval;
+               spl_t x;
+
+               if (thread->options & TH_OPT_IDLE_THREAD)
+                       continue;
+
+               x = splsched();
+               thread_lock(thread);
+
+               if ((thread->state & TH_RUN) == TH_RUN)
+                       numrunning++;
+               cswitch += thread->c_switch;
+               tval = timer_grab(&thread->user_timer);
+               tinfo.threads_user += tval;
+               tinfo.total_user += tval;
+
+               tval = timer_grab(&thread->system_timer);
+
+               if (thread->precise_user_kernel_time) {
+                       tinfo.threads_system += tval;
+                       tinfo.total_system += tval;
+               } else {
+                       /* system_timer may represent either sys or user */
+                       tinfo.threads_user += tval;
+                       tinfo.total_user += tval;
+               }
+
+               syscalls_unix += thread->syscalls_unix;
+               syscalls_mach += thread->syscalls_mach;
+
+               thread_unlock(thread);
+               splx(x);
+       }
+
+       ptinfo->pti_total_system = tinfo.total_system;
+       ptinfo->pti_total_user = tinfo.total_user;
+       ptinfo->pti_threads_system = tinfo.threads_system;
+       ptinfo->pti_threads_user = tinfo.threads_user;
+       
+       ptinfo->pti_faults = task->faults;
+       ptinfo->pti_pageins = task->pageins;
+       ptinfo->pti_cow_faults = task->cow_faults;
+       ptinfo->pti_messages_sent = task->messages_sent;
+       ptinfo->pti_messages_received = task->messages_received;
+       ptinfo->pti_syscalls_mach = task->syscalls_mach + syscalls_mach;
+       ptinfo->pti_syscalls_unix = task->syscalls_unix + syscalls_unix;
+       ptinfo->pti_csw = task->c_switch + cswitch;
+       ptinfo->pti_threadnum = task->thread_count;
+       ptinfo->pti_numrunning = numrunning;
+       ptinfo->pti_priority = task->priority;
+
+       task_unlock(task);
+}
+
+int 
+fill_taskthreadinfo(task_t task, uint64_t thaddr, int thuniqueid, struct proc_threadinfo_internal * ptinfo, void * vpp, int *vidp)
+{
+       thread_t  thact;
+       int err=0;
+       mach_msg_type_number_t count;
+       thread_basic_info_data_t basic_info;
+       kern_return_t kret;
+       uint64_t addr = 0;
+
+       task_lock(task);
+
+       for (thact  = (thread_t)(void *)queue_first(&task->threads);
+                       !queue_end(&task->threads, (queue_entry_t)thact); ) {
+               addr = (thuniqueid==0)?thact->machine.cthread_self: thact->thread_id;
+               if (addr == thaddr)
+               {
+               
+                       count = THREAD_BASIC_INFO_COUNT;
+                       if ((kret = thread_info_internal(thact, THREAD_BASIC_INFO, (thread_info_t)&basic_info, &count)) != KERN_SUCCESS) {
+                               err = 1;
+                               goto out;       
+                       }
+                       ptinfo->pth_user_time = ((basic_info.user_time.seconds * (integer_t)NSEC_PER_SEC) + (basic_info.user_time.microseconds * (integer_t)NSEC_PER_USEC));
+                       ptinfo->pth_system_time = ((basic_info.system_time.seconds * (integer_t)NSEC_PER_SEC) + (basic_info.system_time.microseconds * (integer_t)NSEC_PER_USEC));
+
+                       ptinfo->pth_cpu_usage = basic_info.cpu_usage;
+                       ptinfo->pth_policy = basic_info.policy;
+                       ptinfo->pth_run_state = basic_info.run_state;
+                       ptinfo->pth_flags = basic_info.flags;
+                       ptinfo->pth_sleep_time = basic_info.sleep_time;
+                       ptinfo->pth_curpri = thact->sched_pri;
+                       ptinfo->pth_priority = thact->priority;
+                       ptinfo->pth_maxpriority = thact->max_priority;
+                       
+                       if ((vpp != NULL) && (thact->uthread != NULL)) 
+                               bsd_threadcdir(thact->uthread, vpp, vidp);
+                       bsd_getthreadname(thact->uthread,ptinfo->pth_name);
+                       err = 0;
+                       goto out; 
+               }
+               thact = (thread_t)(void *)queue_next(&thact->task_threads);
+       }
+       err = 1;
+
+out:
+       task_unlock(task);
+       return(err);
+}
+
+int
+fill_taskthreadlist(task_t task, void * buffer, int thcount)
+{
+       int numthr=0;
+       thread_t thact;
+       uint64_t * uptr;
+       uint64_t  thaddr;
+
+       uptr = (uint64_t *)buffer;
+
+       task_lock(task);
+
+       for (thact  = (thread_t)(void *)queue_first(&task->threads);
+                       !queue_end(&task->threads, (queue_entry_t)thact); ) {
+               thaddr = thact->machine.cthread_self;
+               *uptr++ = thaddr;
+               numthr++;
+               if (numthr >= thcount)
+                       goto out;
+               thact = (thread_t)(void *)queue_next(&thact->task_threads);
+       }
+
+out:
+       task_unlock(task);
+       return (int)(numthr * sizeof(uint64_t));
+       
+}
+
+int
+get_numthreads(task_t task)
+{
+       return(task->thread_count);
+}
+
+void 
+syscall_exit_funnelcheck(void)
+{
+        thread_t thread;
+
+       thread = current_thread();
+
+        if (thread->funnel_lock)
+               panic("syscall exit with funnel held\n");
+}
+
+
+/*
+ * Gather the various pieces of info about the designated task, 
+ * and collect it all into a single rusage_info.
+ */
+int
+fill_task_rusage_v2(task_t task, struct rusage_info_v2 *ri)
+{
+       struct task_power_info powerinfo;
+
+       assert(task != TASK_NULL);
+       task_lock(task);
+
+       task_power_info_locked(task, &powerinfo);
+       ri->ri_pkg_idle_wkups = powerinfo.task_platform_idle_wakeups;
+       ri->ri_interrupt_wkups = powerinfo.task_interrupt_wakeups;
+       ri->ri_user_time = powerinfo.total_user;
+       ri->ri_system_time = powerinfo.total_system;
+
+       ledger_get_balance(task->ledger, task_ledgers.phys_footprint,
+                          (ledger_amount_t *)&ri->ri_phys_footprint);
+       ledger_get_balance(task->ledger, task_ledgers.phys_mem,
+                          (ledger_amount_t *)&ri->ri_resident_size);
+       ledger_get_balance(task->ledger, task_ledgers.wired_mem,
+                          (ledger_amount_t *)&ri->ri_wired_size);
+
+       ri->ri_pageins = task->pageins;
+
+       task_unlock(task);
+       return (0);
+}