X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/2d21ac55c334faf3a56e5634905ed6987fc787d4..316670eb35587141e969394ae8537d66b9211e80:/osfmk/kern/thread.h diff --git a/osfmk/kern/thread.h b/osfmk/kern/thread.h index 8387019b8..b497fa3fa 100644 --- a/osfmk/kern/thread.h +++ b/osfmk/kern/thread.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000-2007 Apple Inc. All rights reserved. + * Copyright (c) 2000-2009 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * @@ -146,6 +146,9 @@ struct thread { #define TH_OPT_INTMASK 0x03 /* interrupt / abort level */ #define TH_OPT_VMPRIV 0x04 /* may allocate reserved memory */ #define TH_OPT_DTRACE 0x08 /* executing under dtrace_probe */ +#define TH_OPT_SYSTEM_CRITICAL 0x10 /* Thread must always be allowed to run - even under heavy load */ +#define TH_OPT_PROC_CPULIMIT 0x20 /* Thread has a task-wide CPU limit applied to it */ +#define TH_OPT_PRVT_CPULIMIT 0x40 /* Thread has a thread-private CPU limit applied to it */ /* Data updated during assert_wait/thread_wakeup */ decl_simple_lock_data(,sched_lock) /* scheduling lock (thread_lock()) */ @@ -178,28 +181,49 @@ struct thread { #define TH_RUN 0x04 /* running or on runq */ #define TH_UNINT 0x08 /* waiting uninteruptibly */ #define TH_TERMINATE 0x10 /* halted at termination */ +#define TH_TERMINATE2 0x20 /* added to termination queue */ #define TH_IDLE 0x80 /* idling processor */ /* Scheduling information */ - integer_t sched_mode; /* scheduling mode bits */ -#define TH_MODE_REALTIME 0x0001 /* time constraints supplied */ -#define TH_MODE_TIMESHARE 0x0002 /* use timesharing algorithm */ -#define TH_MODE_PREEMPT 0x0004 /* can preempt kernel contexts */ -#define TH_MODE_FAILSAFE 0x0008 /* fail-safe has tripped */ -#define TH_MODE_PROMOTED 0x0010 /* sched pri has been promoted */ -#define TH_MODE_ABORT 0x0020 /* abort interruptible waits */ -#define TH_MODE_ABORTSAFELY 0x0040 /* ... but only those at safe point */ -#define TH_MODE_ISABORTED (TH_MODE_ABORT | TH_MODE_ABORTSAFELY) -#define TH_MODE_DEPRESS 0x0080 /* normal depress yield */ -#define TH_MODE_POLLDEPRESS 0x0100 /* polled depress yield */ -#define TH_MODE_ISDEPRESSED (TH_MODE_DEPRESS | TH_MODE_POLLDEPRESS) + sched_mode_t sched_mode; /* scheduling mode */ + sched_mode_t saved_mode; /* saved mode during forced mode demotion */ + + unsigned int sched_flags; /* current flag bits */ +#define TH_SFLAG_FAIRSHARE_TRIPPED 0x0001 /* fairshare scheduling activated */ +#define TH_SFLAG_FAILSAFE 0x0002 /* fail-safe has tripped */ +#define TH_SFLAG_THROTTLED 0x0004 /* owner task in throttled state */ +#define TH_SFLAG_DEMOTED_MASK (TH_SFLAG_THROTTLED | TH_SFLAG_FAILSAFE | TH_SFLAG_FAIRSHARE_TRIPPED) + +#define TH_SFLAG_PROMOTED 0x0008 /* sched pri has been promoted */ +#define TH_SFLAG_ABORT 0x0010 /* abort interruptible waits */ +#define TH_SFLAG_ABORTSAFELY 0x0020 /* ... but only those at safe point */ +#define TH_SFLAG_ABORTED_MASK (TH_SFLAG_ABORT | TH_SFLAG_ABORTSAFELY) +#define TH_SFLAG_DEPRESS 0x0040 /* normal depress yield */ +#define TH_SFLAG_POLLDEPRESS 0x0080 /* polled depress yield */ +#define TH_SFLAG_DEPRESSED_MASK (TH_SFLAG_DEPRESS | TH_SFLAG_POLLDEPRESS) +#define TH_SFLAG_PRI_UPDATE 0x0100 /* Updating priority */ +#define TH_SFLAG_EAGERPREEMPT 0x0200 /* Any preemption of this thread should be treated as if AST_URGENT applied */ + +/* + * A thread can either be completely unthrottled, about to be throttled, + * throttled (TH_SFLAG_THROTTLED), or about to be unthrottled + */ +#define TH_SFLAG_PENDING_THROTTLE_DEMOTION 0x1000 /* Pending sched_mode demotion */ +#define TH_SFLAG_PENDING_THROTTLE_PROMOTION 0x2000 /* Pending sched_mode promition */ +#define TH_SFLAG_PENDING_THROTTLE_MASK (TH_SFLAG_PENDING_THROTTLE_DEMOTION | TH_SFLAG_PENDING_THROTTLE_PROMOTION) integer_t sched_pri; /* scheduled (current) priority */ integer_t priority; /* base priority */ integer_t max_priority; /* max base priority */ integer_t task_priority; /* copy of task base priority */ +#if defined(CONFIG_SCHED_GRRR) +#if 0 + uint16_t grrr_deficit; /* fixed point (1/1000th quantum) fractional deficit */ +#endif +#endif + integer_t promotions; /* level of promotion */ integer_t pending_promoter_index; void *pending_promoter[2]; @@ -216,36 +240,44 @@ struct thread { uint64_t deadline; } realtime; + uint32_t was_promoted_on_wakeup; uint32_t current_quantum; /* duration of current quantum */ + uint64_t last_run_time; /* time when thread was switched away from */ + uint64_t last_quantum_refill_time; /* time when current_quantum was refilled after expiration */ /* Data used during setrun/dispatch */ timer_data_t system_timer; /* system mode timer */ processor_t bound_processor; /* bound to a processor? */ processor_t last_processor; /* processor last dispatched on */ - uint64_t last_switch; /* time of last context switch */ + processor_t chosen_processor; /* Where we want to run this thread */ /* Fail-safe computation since last unblock or qualifying yield */ uint64_t computation_metered; uint64_t computation_epoch; - integer_t safe_mode; /* saved mode during fail-safe */ - natural_t safe_release; /* when to release fail-safe */ + uint64_t safe_release; /* when to release fail-safe */ /* Call out from scheduler */ void (*sched_call)( int type, thread_t thread); - +#if defined(CONFIG_SCHED_PROTO) + uint32_t runqueue_generation; /* last time runqueue was drained */ +#endif + /* Statistics and timesharing calculations */ +#if defined(CONFIG_SCHED_TRADITIONAL) natural_t sched_stamp; /* last scheduler tick */ natural_t sched_usage; /* timesharing cpu usage [sched] */ natural_t pri_shift; /* usage -> priority from pset */ natural_t cpu_usage; /* instrumented cpu usage [%cpu] */ natural_t cpu_delta; /* accumulated cpu_usage delta */ +#endif uint32_t c_switch; /* total context switches */ uint32_t p_switch; /* total processor switches */ uint32_t ps_switch; /* total pset switches */ /* Timing data structures */ + int precise_user_kernel_time; /* precise user/kernel enabled for this thread */ timer_data_t user_timer; /* user mode timer */ uint64_t user_timer_save; /* saved user timer value */ uint64_t system_timer_save; /* saved system timer value */ @@ -278,6 +310,7 @@ struct thread { mach_msg_size_t msize; /* max size for recvd msg */ mach_msg_option_t option; /* options for receive */ mach_msg_size_t slist_size; /* scatter list size */ + mach_port_name_t receiver_name; /* the receive port name */ struct ipc_kmsg *kmsg; /* received message */ mach_port_seqno_t seqno; /* seqno of recvd message */ mach_msg_continue_t continuation; @@ -300,10 +333,10 @@ struct thread { mach_port_t ith_rpc_reply; /* reply port for kernel RPCs */ /* Ast/Halt data structures */ - vm_offset_t recover; /* page fault recover(copyin/out) */ + vm_offset_t recover; /* page fault recover(copyin/out) */ uint32_t ref_count; /* number of references to me */ - queue_chain_t threads; /* global list of all threads */ + queue_chain_t threads; /* global list of all threads */ /* Activation */ queue_chain_t task_threads; @@ -315,7 +348,7 @@ struct thread { struct task *task; vm_map_t map; - decl_mutex_data(,mutex) + decl_lck_mtx_data(,mutex) /* Kernel holds on this thread */ int suspend_count; @@ -358,7 +391,42 @@ struct thread { int64_t t_dtrace_tracing; /* Thread time under dtrace_probe() */ int64_t t_dtrace_vtime; #endif + + uint32_t t_page_creation_count; + clock_sec_t t_page_creation_time; + +#define T_CHUD_MARKED 0x01 /* this thread is marked by CHUD */ +#define T_IN_CHUD 0x02 /* this thread is already in a CHUD handler */ +#define THREAD_PMC_FLAG 0x04 /* Bit in "t_chud" signifying PMC interest */ +#define T_AST_CALLSTACK 0x08 /* Thread scheduled to dump a + * callstack on its next + * AST */ +#define T_AST_NAME 0x10 /* Thread scheduled to dump + * its name on its next + * AST */ +#define T_NAME_DONE 0x20 /* Thread has previously + * recorded its name */ + uint32_t t_chud; /* CHUD flags, used for Shark */ + uint32_t chud_c_switch; /* last dispatch detection */ + + integer_t mutex_count; /* total count of locks held */ + + uint64_t thread_id; /*system wide unique thread-id*/ + + /* Statistics accumulated per-thread and aggregated per-task */ + uint32_t syscalls_unix; + uint32_t syscalls_mach; + ledger_t t_ledger; + ledger_t t_threadledger; /* per thread ledger */ + struct process_policy ext_appliedstate; /* externally applied actions */ + struct process_policy ext_policystate; /* externally defined process policy states*/ + struct process_policy appliedstate; /* self applied acions */ + struct process_policy policystate; /* process wide policy states */ +#if CONFIG_EMBEDDED + task_watch_t * taskwatch; /* task watch */ + integer_t saved_importance; /* saved task-relative importance */ +#endif /* CONFIG_EMBEDDED */ }; #define ith_state saved.receive.state @@ -367,6 +435,7 @@ struct thread { #define ith_msize saved.receive.msize #define ith_option saved.receive.option #define ith_scatter_list_size saved.receive.slist_size +#define ith_receiver_name saved.receive.receiver_name #define ith_continuation saved.receive.continuation #define ith_kmsg saved.receive.kmsg #define ith_seqno saved.receive.seqno @@ -418,6 +487,7 @@ extern void thread_hold( extern void thread_release( thread_t thread); + #define thread_lock_init(th) simple_lock_init(&(th)->sched_lock, 0) #define thread_lock(th) simple_lock(&(th)->sched_lock) #define thread_unlock(th) simple_unlock(&(th)->sched_lock) @@ -431,11 +501,15 @@ extern void thread_release( extern void stack_alloc( thread_t thread); +extern void stack_handoff( + thread_t from, + thread_t to); + extern void stack_free( thread_t thread); -extern void stack_free_stack( - vm_offset_t stack); +extern void stack_free_reserved( + thread_t thread); extern boolean_t stack_alloc_try( thread_t thread); @@ -444,20 +518,6 @@ extern void stack_collect(void); extern void stack_init(void) __attribute__((section("__TEXT, initcode"))); -extern kern_return_t thread_state_initialize( - thread_t thread); - -extern kern_return_t thread_setstatus( - thread_t thread, - int flavor, - thread_state_t tstate, - mach_msg_type_number_t count); - -extern kern_return_t thread_getstatus( - thread_t thread, - int flavor, - thread_state_t tstate, - mach_msg_type_number_t *count); extern kern_return_t thread_info_internal( thread_t thread, @@ -537,14 +597,15 @@ extern void machine_thread_destroy( extern void machine_set_current_thread( thread_t thread); -extern void machine_thread_terminate_self(void); - extern kern_return_t machine_thread_get_kern_state( thread_t thread, thread_flavor_t flavor, thread_state_t tstate, mach_msg_type_number_t *count); +extern kern_return_t machine_thread_inherit_taskwide( + thread_t thread, + task_t parent_task); /* * XXX Funnel locks XXX @@ -560,9 +621,9 @@ struct funnel_lock { typedef struct ReturnHandler ReturnHandler; -#define thread_mtx_lock(thread) mutex_lock(&(thread)->mutex) -#define thread_mtx_try(thread) mutex_try(&(thread)->mutex) -#define thread_mtx_unlock(thread) mutex_unlock(&(thread)->mutex) +#define thread_mtx_lock(thread) lck_mtx_lock(&(thread)->mutex) +#define thread_mtx_try(thread) lck_mtx_try_lock(&(thread)->mutex) +#define thread_mtx_unlock(thread) lck_mtx_unlock(&(thread)->mutex) extern void act_execute_returnhandlers(void); @@ -575,6 +636,15 @@ extern void special_handler( void act_machine_sv_free(thread_t, int); +vm_offset_t min_valid_stack_address(void); +vm_offset_t max_valid_stack_address(void); + +extern void funnel_lock( + struct funnel_lock *lock); + +extern void funnel_unlock( + struct funnel_lock *lock); + #else /* MACH_KERNEL_PRIVATE */ __BEGIN_DECLS @@ -593,33 +663,22 @@ __END_DECLS #ifdef KERNEL_PRIVATE -typedef struct funnel_lock funnel_t; - -#ifdef MACH_KERNEL_PRIVATE - -extern void funnel_lock( - funnel_t *lock); - -extern void funnel_unlock( - funnel_t *lock); - -vm_offset_t min_valid_stack_address(void); -vm_offset_t max_valid_stack_address(void); - -#endif /* MACH_KERNEL_PRIVATE */ - __BEGIN_DECLS -extern funnel_t *thread_funnel_get(void); - -extern boolean_t thread_funnel_set( - funnel_t *lock, - boolean_t funneled); +#if defined(__i386__) extern thread_t kernel_thread( task_t task, void (*start)(void)); +#endif /* defined(__i386__) */ + +extern uint64_t thread_tid( + thread_t thread); + +extern uint64_t thread_dispatchqaddr( + thread_t thread); + __END_DECLS #endif /* KERNEL_PRIVATE */ @@ -628,12 +687,47 @@ __BEGIN_DECLS #ifdef XNU_KERNEL_PRIVATE +extern kern_return_t thread_state_initialize( + thread_t thread); + +extern kern_return_t thread_setstatus( + thread_t thread, + int flavor, + thread_state_t tstate, + mach_msg_type_number_t count); + +extern kern_return_t thread_getstatus( + thread_t thread, + int flavor, + thread_state_t tstate, + mach_msg_type_number_t *count); + +extern kern_return_t thread_create_workq( + task_t task, + thread_continue_t thread_return, + thread_t *new_thread); + extern void thread_yield_internal( mach_msg_timeout_t interval); /* - * XXX Funnel locks XXX + * Thread-private CPU limits: apply a private CPU limit to this thread only. Available actions are: + * + * 1) Block. Prevent CPU consumption of the thread from exceeding the limit. + * 2) Exception. Generate a resource consumption exception when the limit is exceeded. */ +#define THREAD_CPULIMIT_BLOCK 0x1 +#define THREAD_CPULIMIT_EXCEPTION 0x2 + +struct _thread_ledger_indices { + int cpu_time; +}; + +extern struct _thread_ledger_indices thread_ledgers; + +extern int thread_set_cpulimit(int action, uint8_t percentage, uint64_t interval_ns); + +typedef struct funnel_lock funnel_t; #define THR_FUNNEL_NULL (funnel_t *)0 @@ -643,6 +737,12 @@ extern funnel_t *funnel_alloc( extern void funnel_free( funnel_t *lock); +extern funnel_t *thread_funnel_get(void); + +extern boolean_t thread_funnel_set( + funnel_t *lock, + boolean_t funneled); + extern void thread_read_times( thread_t thread, time_value_t *user_time, @@ -664,6 +764,25 @@ extern kern_return_t thread_setsinglestep( thread_t thread, int on); +extern kern_return_t thread_userstack( + thread_t, + int, + thread_state_t, + unsigned int, + mach_vm_offset_t *, + int *); + +extern kern_return_t thread_entrypoint( + thread_t, + int, + thread_state_t, + unsigned int, + mach_vm_offset_t *); + +extern kern_return_t thread_userstackdefault( + thread_t, + mach_vm_offset_t *); + extern kern_return_t thread_wire_internal( host_priv_t host_priv, thread_t thread, @@ -687,6 +806,13 @@ extern void thread_static_param( thread_t thread, boolean_t state); +extern kern_return_t thread_policy_set_internal( + thread_t thread, + thread_policy_flavor_t flavor, + thread_policy_t policy_info, + mach_msg_type_number_t count); + + extern task_t get_threadtask(thread_t); #define thread_is_64bit(thd) \ task_has_64BitAddr(get_threadtask(thd)) @@ -694,7 +820,7 @@ extern task_t get_threadtask(thread_t); extern void *get_bsdthread_info(thread_t); extern void set_bsdthread_info(thread_t, void *); -extern void *uthread_alloc(task_t, thread_t); +extern void *uthread_alloc(task_t, thread_t, int); extern void uthread_cleanup(task_t, void *, void *); extern void uthread_zone_free(void *); extern void uthread_cred_free(void *); @@ -702,9 +828,13 @@ extern void uthread_cred_free(void *); extern boolean_t thread_should_halt( thread_t thread); +extern boolean_t thread_should_abort( + thread_t); + extern int is_64signalregset(void); void act_set_apc(thread_t); +void act_set_kperf(thread_t); extern uint32_t dtrace_get_thread_predcache(thread_t); extern int64_t dtrace_get_thread_vtime(thread_t); @@ -716,6 +846,7 @@ extern void dtrace_set_thread_vtime(thread_t, int64_t); extern void dtrace_set_thread_tracing(thread_t, int64_t); extern void dtrace_set_thread_reentering(thread_t, boolean_t); extern vm_offset_t dtrace_set_thread_recover(thread_t, vm_offset_t); +extern void dtrace_thread_bootstrap(void); extern int64_t dtrace_calc_thread_recent_vtime(thread_t); @@ -728,12 +859,30 @@ extern void thread_set_wq_state64( thread_t thread, thread_state_t tstate); +extern vm_offset_t kernel_stack_mask; +extern vm_offset_t kernel_stack_size; +extern vm_offset_t kernel_stack_depth_max; + #endif /* XNU_KERNEL_PRIVATE */ +/*! @function kernel_thread_start + @abstract Create a kernel thread. + @discussion This function takes three input parameters, namely reference to the function that the thread should execute, caller specified data and a reference which is used to return the newly created kernel thread. The function returns KERN_SUCCESS on success or an appropriate kernel code type indicating the error. It may be noted that the caller is responsible for explicitly releasing the reference to the created thread when no longer needed. This should be done by calling thread_deallocate(new_thread). + @param continuation A C-function pointer where the thread will begin execution. + @param parameter Caller specified data to be passed to the new thread. + @param new_thread Reference to the new thread is returned in this parameter. + @result Returns KERN_SUCCESS on success or an appropriate kernel code type. +*/ + extern kern_return_t kernel_thread_start( thread_continue_t continuation, void *parameter, thread_t *new_thread); +#ifdef KERNEL_PRIVATE +void thread_set_eager_preempt(thread_t thread); +void thread_clear_eager_preempt(thread_t thread); +extern ipc_port_t convert_thread_to_port(thread_t); +#endif /* KERNEL_PRIVATE */ __END_DECLS