]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/bsd_kern.c
xnu-3789.31.2.tar.gz
[apple/xnu.git] / osfmk / kern / bsd_kern.c
index 3fc7a40a71a78c040efad7800e6e88e11da93c89..ac79a2345df76e61352f1c47b1d5b3f5badfd3ab 100644 (file)
 #include <vm/pmap.h>
 #include <vm/vm_protos.h> /* last */
 #include <sys/resource.h>
+#include <sys/signal.h>
 
 #undef thread_should_halt
 
 /* 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_t get_firstthread(task_t);
@@ -58,11 +58,18 @@ int get_thread_userstop(thread_t);
 boolean_t current_thread_aborted(void);
 void task_act_iterate_wth_args(task_t, void(*)(thread_t, void *), 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);
 int fill_task_rusage(task_t task, rusage_info_current *ri);
 int fill_task_io_rusage(task_t task, rusage_info_current *ri);
 int fill_task_qos_rusage(task_t task, rusage_info_current *ri);
 void fill_task_billed_usage(task_t task, rusage_info_current *ri);
+void task_bsdtask_kill(task_t);
+
+extern uint64_t get_dispatchqueue_serialno_offset_from_proc(void *p);
+extern uint64_t proc_uniqueid(void *p);
+
+#if MACH_BSD
+extern void psignal(void *, int);
+#endif
 
 /*
  *
@@ -72,6 +79,13 @@ void  *get_bsdtask_info(task_t t)
        return(t->bsd_info);
 }
 
+void task_bsdtask_kill(task_t t)
+{
+       void * bsd_info = get_bsdtask_info(t);
+       if (bsd_info != NULL) {
+               psignal(bsd_info, SIGKILL);
+       }
+}
 /*
  *
  */
@@ -226,9 +240,9 @@ ledger_t  get_task_ledger(task_t t)
 
 /*
  * This is only safe to call from a thread executing in
- * in the task's context or if the task is locked  Otherwise,
+ * 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.
+ * we go to return it here.
  */
 vm_map_t  get_task_map(task_t t)
 {
@@ -294,20 +308,24 @@ int is_64signalregset(void)
 
 /*
  * Swap in a new map for the task/thread pair; the old map reference is
- * returned.
+ * returned. Also does a pmap switch if thread provided is current thread.
  */
 vm_map_t
-swap_task_map(task_t task, thread_t thread, vm_map_t map, boolean_t doswitch)
+swap_task_map(task_t task, thread_t thread, vm_map_t map)
 {
        vm_map_t old_map;
+       boolean_t doswitch = (thread == current_thread()) ? TRUE : FALSE;
 
        if (task != thread->task)
                panic("swap_task_map");
 
        task_lock(task);
        mp_disable_preemption();
+
        old_map = task->map;
        thread->map = task->map = map;
+       vm_commit_pagezero_status(map);
+
        if (doswitch) {
                pmap_switch(map->pmap);
        }
@@ -323,6 +341,10 @@ swap_task_map(task_t task, thread_t thread, vm_map_t map, boolean_t doswitch)
 
 /*
  *
+ * 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 go to return it here.
  */
 pmap_t  get_task_pmap(task_t t)
 {
@@ -358,16 +380,27 @@ uint64_t get_task_resident_max(task_t task)
 
 uint64_t get_task_purgeable_size(task_t task) 
 {
-       vm_map_t map;
-    mach_vm_size_t  volatile_virtual_size;
-    mach_vm_size_t  volatile_resident_size;
-    mach_vm_size_t  volatile_pmap_size;
-       
-       map = (task == kernel_task) ? kernel_map: task->map;
-       vm_map_query_volatile(map, &volatile_virtual_size, &volatile_resident_size, &volatile_pmap_size);
+       kern_return_t ret;
+       ledger_amount_t credit, debit;
+       uint64_t volatile_size = 0;
+
+       ret = ledger_get_entries(task->ledger, task_ledgers.purgeable_volatile, &credit, &debit);
+       if (ret != KERN_SUCCESS) {
+               return 0;
+       }
+
+       volatile_size += (credit - debit);
+
+       ret = ledger_get_entries(task->ledger, task_ledgers.purgeable_volatile_compressed, &credit, &debit);
+       if (ret != KERN_SUCCESS) {
+               return 0;
+       }
+
+       volatile_size += (credit - debit);
 
-       return((uint64_t)volatile_resident_size);
+       return volatile_size;
 }
+
 /*
  *
  */
@@ -400,6 +433,126 @@ uint64_t get_task_phys_footprint_max(task_t task)
        return 0;
 }
 
+/*
+ *
+ */
+uint64_t get_task_phys_footprint_limit(task_t task)
+{
+       kern_return_t ret;
+       ledger_amount_t max;
+
+       ret = ledger_get_limit(task->ledger, task_ledgers.phys_footprint, &max);
+       if (KERN_SUCCESS == ret) {
+               return max;
+       }
+
+       return 0;
+}
+
+uint64_t get_task_internal(task_t task)
+{
+       kern_return_t ret;
+       ledger_amount_t credit, debit;
+
+       ret = ledger_get_entries(task->ledger, task_ledgers.internal, &credit, &debit);
+       if (KERN_SUCCESS == ret) {
+               return (credit - debit);
+       }
+
+       return 0;
+}
+
+uint64_t get_task_internal_compressed(task_t task)
+{
+       kern_return_t ret;
+       ledger_amount_t credit, debit;
+
+       ret = ledger_get_entries(task->ledger, task_ledgers.internal_compressed, &credit, &debit);
+       if (KERN_SUCCESS == ret) {
+               return (credit - debit);
+       }
+
+       return 0;
+}
+
+uint64_t get_task_purgeable_nonvolatile(task_t task)
+{
+       kern_return_t ret;
+       ledger_amount_t credit, debit;
+
+       ret = ledger_get_entries(task->ledger, task_ledgers.purgeable_nonvolatile, &credit, &debit);
+       if (KERN_SUCCESS == ret) {
+               return (credit - debit);
+       }
+
+       return 0;
+}
+
+uint64_t get_task_purgeable_nonvolatile_compressed(task_t task)
+{
+       kern_return_t ret;
+       ledger_amount_t credit, debit;
+
+       ret = ledger_get_entries(task->ledger, task_ledgers.purgeable_nonvolatile_compressed, &credit, &debit);
+       if (KERN_SUCCESS == ret) {
+               return (credit - debit);
+       }
+
+       return 0;
+}
+
+uint64_t get_task_alternate_accounting(task_t task)
+{
+       kern_return_t ret;
+       ledger_amount_t credit, debit;
+
+       ret = ledger_get_entries(task->ledger, task_ledgers.alternate_accounting, &credit, &debit);
+       if (KERN_SUCCESS == ret) {
+               return (credit - debit);
+       }
+
+       return 0;
+}
+
+uint64_t get_task_alternate_accounting_compressed(task_t task)
+{
+       kern_return_t ret;
+       ledger_amount_t credit, debit;
+
+       ret = ledger_get_entries(task->ledger, task_ledgers.alternate_accounting_compressed, &credit, &debit);
+       if (KERN_SUCCESS == ret) {
+               return (credit - debit);
+       }
+
+       return 0;
+}
+
+uint64_t get_task_page_table(task_t task)
+{
+       kern_return_t ret;
+       ledger_amount_t credit, debit;
+
+       ret = ledger_get_entries(task->ledger, task_ledgers.page_table, &credit, &debit);
+       if (KERN_SUCCESS == ret) {
+               return (credit - debit);
+       }
+
+       return 0;
+}
+
+uint64_t get_task_iokit_mapped(task_t task)
+{
+       kern_return_t ret;
+       ledger_amount_t credit, debit;
+
+       ret = ledger_get_entries(task->ledger, task_ledgers.iokit_mapped, &credit, &debit);
+       if (KERN_SUCCESS == ret) {
+               return (credit - debit);
+       }
+
+       return 0;
+}
+
 uint64_t get_task_cpu_time(task_t task)
 {
        kern_return_t ret;
@@ -413,13 +566,6 @@ uint64_t get_task_cpu_time(task_t task)
        return 0;
 }
 
-/*
- *
- */
-pmap_t  get_map_pmap(vm_map_t map)
-{
-       return(map->pmap);
-}
 /*
  *
  */
@@ -454,7 +600,9 @@ get_vmmap_size(
        return(map->size);
 }
 
-int
+#if CONFIG_COREDUMP
+
+static int
 get_vmsubmap_entries(
        vm_map_t        map,
        vm_object_offset_t      start,
@@ -473,10 +621,11 @@ get_vmsubmap_entries(
        while((entry != vm_map_to_entry(map)) && (entry->vme_start < end)) {
                if(entry->is_sub_map) {
                        total_entries +=        
-                               get_vmsubmap_entries(entry->object.sub_map, 
-                                       entry->offset, 
-                                       entry->offset + 
-                                       (entry->vme_end - entry->vme_start));
+                               get_vmsubmap_entries(VME_SUBMAP(entry), 
+                                                    VME_OFFSET(entry), 
+                                                    (VME_OFFSET(entry) + 
+                                                     entry->vme_end -
+                                                     entry->vme_start));
                } else {
                        total_entries += 1;
                }
@@ -501,10 +650,11 @@ get_vmmap_entries(
        while(entry != vm_map_to_entry(map)) {
                if(entry->is_sub_map) {
                        total_entries +=        
-                               get_vmsubmap_entries(entry->object.sub_map, 
-                                       entry->offset, 
-                                       entry->offset + 
-                                       (entry->vme_end - entry->vme_start));
+                               get_vmsubmap_entries(VME_SUBMAP(entry), 
+                                                    VME_OFFSET(entry),
+                                                    (VME_OFFSET(entry) + 
+                                                     entry->vme_end -
+                                                     entry->vme_start));
                } else {
                        total_entries += 1;
                }
@@ -514,6 +664,7 @@ get_vmmap_entries(
          vm_map_unlock(map);
        return(total_entries);
 }
+#endif /* CONFIG_COREDUMP */
 
 /*
  *
@@ -620,17 +771,6 @@ task_act_iterate_wth_args(
 }
 
 
-void
-astbsd_on(void)
-{
-       boolean_t       reenable;
-
-       reenable = ml_set_interrupts_enabled(FALSE);
-       ast_on_fast(AST_BSD);
-       (void)ml_set_interrupts_enabled(reenable);
-}
-
-
 #include <sys/bsdtask_info.h>
 
 void
@@ -643,6 +783,8 @@ fill_taskprocinfo(task_t task, struct proc_taskinfo_internal * ptinfo)
        uint32_t syscalls_unix = 0;
        uint32_t syscalls_mach = 0;
 
+       task_lock(task);
+
        map = (task == kernel_task)? kernel_map: task->map;
 
        ptinfo->pti_virtual_size  = map->size;
@@ -650,8 +792,6 @@ fill_taskprocinfo(task_t task, struct proc_taskinfo_internal * ptinfo)
                (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);
 
@@ -746,7 +886,7 @@ fill_taskthreadinfo(task_t task, uint64_t thaddr, int thuniqueid, struct proc_th
                        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_priority = thact->base_pri;
                        ptinfo->pth_maxpriority = thact->max_priority;
                        
                        if ((vpp != NULL) && (thact->uthread != NULL)) 
@@ -810,7 +950,7 @@ fill_task_rusage(task_t task, rusage_info_current *ri)
        assert(task != TASK_NULL);
        task_lock(task);
 
-       task_power_info_locked(task, &powerinfo, NULL);
+       task_power_info_locked(task, &powerinfo, NULL, NULL);
        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;
@@ -833,8 +973,8 @@ void
 fill_task_billed_usage(task_t task __unused, rusage_info_current *ri)
 {
 #if CONFIG_BANK
-       ri->ri_billed_system_time = bank_billed_time(task->bank_context);
-       ri->ri_serviced_system_time = bank_serviced_time(task->bank_context);
+       ri->ri_billed_system_time = bank_billed_time_safe(task);
+       ri->ri_serviced_system_time = bank_serviced_time_safe(task);
 #else
        ri->ri_billed_system_time = 0;
        ri->ri_serviced_system_time = 0;
@@ -872,10 +1012,7 @@ fill_task_qos_rusage(task_t task, rusage_info_current *ri)
                if (thread->options & TH_OPT_IDLE_THREAD)
                        continue;
 
-               thread_mtx_lock(thread);
-               thread_update_qos_cpu_time(thread, TRUE);
-               thread_mtx_unlock(thread);
-               
+               thread_update_qos_cpu_time(thread);
        }
        ri->ri_cpu_time_qos_default = task->cpu_time_qos_stats.cpu_time_qos_default;
        ri->ri_cpu_time_qos_maintenance = task->cpu_time_qos_stats.cpu_time_qos_maintenance;
@@ -888,3 +1025,39 @@ fill_task_qos_rusage(task_t task, rusage_info_current *ri)
        task_unlock(task);
        return (0);
 }
+
+uint64_t
+get_task_dispatchqueue_serialno_offset(task_t task)
+{
+       uint64_t dq_serialno_offset = 0;
+
+       if (task->bsd_info) {
+               dq_serialno_offset = get_dispatchqueue_serialno_offset_from_proc(task->bsd_info);
+       }
+
+       return dq_serialno_offset;
+}
+
+uint64_t
+get_task_uniqueid(task_t task)
+{
+       if (task->bsd_info) {
+               return proc_uniqueid(task->bsd_info);
+       } else {
+               return UINT64_MAX;
+       }
+}
+
+#if CONFIG_MACF
+struct label *
+get_task_crash_label(task_t task)
+{
+       return task->crash_label;
+}
+
+void
+set_task_crash_label(task_t task, struct label *label)
+{
+       task->crash_label = label;
+}
+#endif