]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/bsd_kern.c
xnu-792.6.22.tar.gz
[apple/xnu.git] / osfmk / kern / bsd_kern.c
index 6b27ea5a9ac41e32f31b32fe8c4cdfd4baa96ff7..57c4559d207ef97aa8bef91540c5a6ce41d11292 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
 #include <mach/mach_types.h>
-#include <kern/queue.h>
-#include <kern/ast.h>
+
+#include <kern/kern_types.h>
+#include <kern/processor.h>
 #include <kern/thread.h>
-#include <kern/thread_act.h>
 #include <kern/task.h>
 #include <kern/spl.h>
 #include <kern/lock.h>
-#include <vm/vm_map.h>
-#include <vm/pmap.h>
+#include <kern/ast.h>
 #include <ipc/ipc_port.h>
 #include <ipc/ipc_object.h>
+#include <vm/vm_map.h>
+#include <vm/pmap.h>
+#include <vm/vm_protos.h> /* last */
 
 #undef thread_should_halt
 #undef ipc_port_release
 
-decl_simple_lock_data(extern,reaper_lock)
-extern queue_head_t           reaper_queue;
-
 /* BSD KERN COMPONENT INTERFACE */
 
 task_t bsd_init_task = TASK_NULL;
 char   init_task_failure_data[1024];
+extern unsigned int not_in_kdp; /* Skip acquiring locks if we're in kdp */
  
-thread_act_t get_firstthread(task_t);
-vm_map_t  get_task_map(task_t);
-ipc_space_t  get_task_ipcspace(task_t);
-boolean_t is_kerneltask(task_t);
-boolean_t is_thread_idle(thread_t);
-boolean_t is_thread_running(thread_act_t);
-thread_shuttle_t getshuttle_thread( thread_act_t);
-thread_act_t getact_thread( thread_shuttle_t);
-vm_offset_t get_map_min( vm_map_t);
-vm_offset_t get_map_max( vm_map_t);
+thread_t get_firstthread(task_t);
 int get_task_userstop(task_t);
-int get_thread_userstop(thread_act_t);
-boolean_t thread_should_abort(thread_shuttle_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_act_t, void *), 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_thread_waitresult(thread_t);
-vm_size_t get_vmmap_size(vm_map_t);
-int get_vmmap_entries(vm_map_t);
-int  get_task_numacts(task_t);
-thread_act_t get_firstthread(task_t task);
-kern_return_t get_signalact(task_t , thread_act_t *, thread_t *, int);
-void astbsd_on(void);
+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);
 
 /*
  *
@@ -87,7 +73,7 @@ void set_bsdtask_info(task_t t,void * v)
 /*
  *
  */
-void *get_bsdthread_info(thread_act_t th)
+void *get_bsdthread_info(thread_t th)
 {
        return(th->uthread);
 }
@@ -98,126 +84,143 @@ void *get_bsdthread_info(thread_act_t th)
  * can't go away, so we make sure it is still active after
  * retrieving the first thread for extra safety.
  */
-thread_act_t get_firstthread(task_t task)
+thread_t get_firstthread(task_t task)
 {
-       thread_act_t    thr_act;
+       thread_t        thread = (thread_t)queue_first(&task->threads);
+
+       if (queue_end(&task->threads, (queue_entry_t)thread))
+               thread = THREAD_NULL;
 
-       thr_act = (thread_act_t)queue_first(&task->thr_acts);
-       if (thr_act == (thread_act_t)&task->thr_acts)
-               thr_act = THR_ACT_NULL;
        if (!task->active)
-               return(THR_ACT_NULL);
-       return(thr_act);
+               return (THREAD_NULL);
+
+       return (thread);
 }
 
-kern_return_t get_signalact(task_t task,thread_act_t * thact, thread_t * thshut, int setast)
+kern_return_t
+get_signalact(
+       task_t          task,
+       thread_t        *result_out,
+       int                     setast)
 {
-
-        thread_act_t inc;
-        thread_act_t ninc;
-        thread_act_t thr_act;
-       thread_t        th;
+       kern_return_t   result = KERN_SUCCESS;
+       thread_t                inc, thread = THREAD_NULL;
 
        task_lock(task);
+
        if (!task->active) {
                task_unlock(task);
-               return(KERN_FAILURE);
+
+               return (KERN_FAILURE);
        }
 
-        thr_act = THR_ACT_NULL;
-        for (inc  = (thread_act_t)queue_first(&task->thr_acts);
-             inc != (thread_act_t)&task->thr_acts;
-             inc  = ninc) {
-                th = act_lock_thread(inc);
-                if ((inc->active)  && 
-                    ((th->state & (TH_ABORT|TH_ABORT_SAFELY)) != TH_ABORT)) {
-                    thr_act = inc;
-                   break;
+       for (inc  = (thread_t)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;
                 }
-                act_unlock_thread(inc);
-                ninc = (thread_act_t)queue_next(&inc->thr_acts);
-        }
-out:
-        if (thact) 
-                *thact = thr_act;
-
-        if (thshut)
-                *thshut = thr_act? thr_act->thread: THREAD_NULL ;
-        if (thr_act) {
-                if (setast)
-                    act_set_astbsd(thr_act);
-
-                act_unlock_thread(thr_act);
-        }
+                thread_mtx_unlock(inc);
+
+                               inc = (thread_t)queue_next(&inc->task_threads);
+       }
+
+       if (result_out) 
+               *result_out = thread;
+
+       if (thread) {
+               if (setast)
+                       act_set_astbsd(thread);
+
+               thread_mtx_unlock(thread);
+       }
+       else
+               result = KERN_FAILURE;
+
        task_unlock(task);
 
-        if (thr_act) 
-            return(KERN_SUCCESS);
-        else 
-            return(KERN_FAILURE);
+       return (result);
 }
 
 
-kern_return_t check_actforsig(task_t task, thread_act_t thact, thread_t * thshut, int setast)
+kern_return_t
+check_actforsig(
+       task_t                  task,
+       thread_t                thread,
+       int                             setast)
 {
-
-        thread_act_t inc;
-        thread_act_t ninc;
-        thread_act_t thr_act;
-               thread_t        th;
-               int found=0;
+       kern_return_t   result = KERN_FAILURE;
+       thread_t                inc;
 
        task_lock(task);
+
        if (!task->active) {
                task_unlock(task);
-               return(KERN_FAILURE);
+
+               return (KERN_FAILURE);
        }
 
-        thr_act = THR_ACT_NULL;
-        for (inc  = (thread_act_t)queue_first(&task->thr_acts);
-             inc != (thread_act_t)&task->thr_acts;
-             inc  = ninc) {
-
-                               if (inc != thact) {
-                       ninc = (thread_act_t)queue_next(&inc->thr_acts);
-                                               continue;
-                               }
-                th = act_lock_thread(inc);
-                if ((inc->active)  && 
-                    ((th->state & (TH_ABORT|TH_ABORT_SAFELY)) != TH_ABORT)) {
-                                       found = 1;
-                    thr_act = inc;
-                   break;
-                }
-                act_unlock_thread(inc);
-                /* ninc = (thread_act_t)queue_next(&inc->thr_acts); */
+       for (inc  = (thread_t)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) {
+                               result = KERN_SUCCESS;
                                break;
-        }
-out:
-               if (found) {
-               if (thshut)
-                       *thshut = thr_act? thr_act->thread: THREAD_NULL ;
-            if (setast)
-                               act_set_astbsd(thr_act);
-
-           act_unlock_thread(thr_act);
-        }
-               task_unlock(task);
+                       }
+
+                       thread_mtx_unlock(inc);
+                       break;
+               }
+
+               inc = (thread_t)queue_next(&inc->task_threads);
+       }
 
-        if (found) 
-            return(KERN_SUCCESS);
-        else 
-            return(KERN_FAILURE);
+       if (result == KERN_SUCCESS) {
+               if (setast)
+                       act_set_astbsd(thread);
+
+               thread_mtx_unlock(thread);
+       }
+
+       task_unlock(task);
+
+       return (result);
 }
 
 /*
- *
+ * This is only safe to call from a thread executing in
+ * in the task's context or if the task is locked  Otherwise,
+ * the map could be switched for the task (and freed) before
+ * we to return it here.
  */
 vm_map_t  get_task_map(task_t t)
 {
        return(t->map);
 }
 
+vm_map_t  get_task_map_reference(task_t t)
+{
+       vm_map_t m;
+
+       if (t == NULL)
+               return VM_MAP_NULL;
+
+       task_lock(t);
+       if (!t->active) {
+               task_unlock(t);
+               return VM_MAP_NULL;
+       }
+       m = t->map;
+       vm_map_reference_swap(m);
+       task_unlock(t);
+       return m;
+}
+
 /*
  *
  */
@@ -228,43 +231,38 @@ ipc_space_t  get_task_ipcspace(task_t t)
 
 int  get_task_numacts(task_t t)
 {
-       return(t->thr_act_count);
+       return(t->thread_count);
+}
+
+/* does this machine need  64bit register set for signal handler */
+int is_64signalregset(void)
+{
+       task_t t = current_task();
+       if(t->taskFeatures[0] & tf64BitData)
+               return(1);
+       else
+               return(0);
 }
 
 /*
- * Reset the current task's map by taking a reference
- * on the new map.  The old map reference is returned.
+ * The old map reference is returned.
  */
 vm_map_t
 swap_task_map(task_t task,vm_map_t map)
 {
+       thread_t thread = current_thread();
        vm_map_t old_map;
 
-       vm_map_reference(map);
+       if (task != thread->task)
+               panic("swap_task_map");
+
        task_lock(task);
        old_map = task->map;
-       task->map = map;
+       thread->map = task->map = map;
        task_unlock(task);
        return old_map;
 }
 
-/*
- * Reset the current act map.
- * The caller donates us a reference to the new map
- * and we donote our reference to the old map to him.
- */
-vm_map_t
-swap_act_map(thread_act_t thr_act,vm_map_t map)
-{
-       vm_map_t old_map;
-
-       act_lock(thr_act);
-       old_map = thr_act->map;
-       thr_act->map = map;
-       act_unlock(thr_act);
-       return old_map;
-}
-
 /*
  *
  */
@@ -283,7 +281,7 @@ pmap_t  get_map_pmap(vm_map_t map)
 /*
  *
  */
-task_t get_threadtask(thread_act_t th)
+task_t get_threadtask(thread_t th)
 {
        return(th->task);
 }
@@ -300,42 +298,35 @@ boolean_t is_thread_idle(thread_t th)
 /*
  *
  */
-boolean_t is_thread_running(thread_act_t thact)
+boolean_t is_thread_running(thread_t th)
 {
-       thread_t th = thact->thread;
        return((th->state & TH_RUN) == TH_RUN);
 }
 
 /*
  *
  */
-thread_shuttle_t
+thread_t
 getshuttle_thread(
-       thread_act_t    th)
+       thread_t        th)
 {
-#ifdef DEBUG
-       assert(th->thread);
-#endif
-       return(th->thread);
+       return(th);
 }
 
 /*
  *
  */
-thread_act_t
+thread_t
 getact_thread(
-       thread_shuttle_t        th)
+       thread_t        th)
 {
-#ifdef DEBUG
-       assert(th->top_act);
-#endif
-       return(th->top_act);
+       return(th);
 }
 
 /*
  *
  */
-vm_offset_t
+vm_map_offset_t
 get_map_min(
        vm_map_t        map)
 {
@@ -345,13 +336,13 @@ get_map_min(
 /*
  *
  */
-vm_offset_t
+vm_map_offset_t
 get_map_max(
        vm_map_t        map)
 {
        return(vm_map_max(map));
 }
-vm_size_t
+vm_map_size_t
 get_vmmap_size(
        vm_map_t        map)
 {
@@ -367,7 +358,8 @@ get_vmsubmap_entries(
        int     total_entries = 0;
        vm_map_entry_t  entry;
 
-       vm_map_lock(map);
+       if (not_in_kdp)
+         vm_map_lock(map);
        entry = vm_map_first_entry(map);
        while((entry != vm_map_to_entry(map)) && (entry->vme_start < start)) {
                entry = entry->vme_next;
@@ -385,7 +377,8 @@ get_vmsubmap_entries(
                }
                entry = entry->vme_next;
        }
-       vm_map_unlock(map);
+       if (not_in_kdp)
+         vm_map_unlock(map);
        return(total_entries);
 }
 
@@ -396,7 +389,8 @@ get_vmmap_entries(
        int     total_entries = 0;
        vm_map_entry_t  entry;
 
-       vm_map_lock(map);
+       if (not_in_kdp)
+         vm_map_lock(map);
        entry = vm_map_first_entry(map);
 
        while(entry != vm_map_to_entry(map)) {
@@ -411,7 +405,8 @@ get_vmmap_entries(
                }
                entry = entry->vme_next;
        }
-       vm_map_unlock(map);
+       if (not_in_kdp)
+         vm_map_unlock(map);
        return(total_entries);
 }
 
@@ -433,7 +428,7 @@ get_task_userstop(
  */
 int
 get_thread_userstop(
-       thread_act_t th)
+       thread_t th)
 {
        return(th->user_stop_count);
 }
@@ -443,10 +438,9 @@ get_thread_userstop(
  */
 boolean_t
 thread_should_abort(
-       thread_shuttle_t th)
+       thread_t th)
 {
-       return(!th->top_act || !th->top_act->active || 
-              (th->state & (TH_ABORT|TH_ABORT_SAFELY)) == TH_ABORT);
+       return ((th->state & (TH_ABORT|TH_ABORT_SAFELY)) == TH_ABORT);
 }
 
 /*
@@ -464,9 +458,8 @@ current_thread_aborted (
        thread_t th = current_thread();
        spl_t s;
 
-       if (!th->top_act || 
-               ((th->state & (TH_ABORT|TH_ABORT_SAFELY)) == TH_ABORT &&
-                th->interrupt_level != THREAD_UNINT))
+       if ((th->state & (TH_ABORT|TH_ABORT_SAFELY)) == TH_ABORT &&
+                       (th->options & TH_OPT_INTMASK) != THREAD_UNINT)
                return (TRUE);
        if (th->state & TH_ABORT_SAFELY) {
                s = splsched();
@@ -484,19 +477,20 @@ current_thread_aborted (
  */
 void
 task_act_iterate_wth_args(
-       task_t task,
-       void (*func_callback)(thread_act_t, void *),
-       void *func_arg)
+       task_t                  task,
+       void                    (*func_callback)(thread_t, void *),
+       void                    *func_arg)
 {
-        thread_act_t inc, ninc;
+       thread_t        inc;
 
        task_lock(task);
-        for (inc  = (thread_act_t)queue_first(&task->thr_acts);
-             inc != (thread_act_t)&task->thr_acts;
-             inc  = ninc) {
-                ninc = (thread_act_t)queue_next(&inc->thr_acts);
-                (void) (*func_callback)(inc, func_arg);
-        }
+
+       for (inc  = (thread_t)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);
+       }
+
        task_unlock(task);
 }
 
@@ -509,18 +503,11 @@ ipc_port_release(
 
 boolean_t
 is_thread_active(
-       thread_shuttle_t th)
+       thread_t th)
 {
        return(th->active);
 }
 
-kern_return_t
-get_thread_waitresult(
-       thread_shuttle_t th)
-{
-       return(th->wait_result);
-}
-
 void
 astbsd_on(void)
 {