2  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. 
   4  * @APPLE_LICENSE_HEADER_START@ 
   6  * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved. 
   8  * This file contains Original Code and/or Modifications of Original Code 
   9  * as defined in and that are subject to the Apple Public Source License 
  10  * Version 2.0 (the 'License'). You may not use this file except in 
  11  * compliance with the License. Please obtain a copy of the License at 
  12  * http://www.opensource.apple.com/apsl/ and read it before using this 
  15  * The Original Code and all software distributed under the License are 
  16  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  17  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  18  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  19  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  20  * Please see the License for the specific language governing rights and 
  21  * limitations under the License. 
  23  * @APPLE_LICENSE_HEADER_END@ 
  29  * Mach Operating System 
  30  * Copyright (c) 1991,1990,1989 Carnegie Mellon University 
  31  * All Rights Reserved. 
  33  * Permission to use, copy, modify and distribute this software and its 
  34  * documentation is hereby granted, provided that both the copyright 
  35  * notice and this permission notice appear in all copies of the 
  36  * software, derivative works or modified versions, and any portions 
  37  * thereof, and that both notices appear in supporting documentation. 
  39  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 
  40  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 
  41  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 
  43  * Carnegie Mellon requests users of this software to return to 
  45  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU 
  46  *  School of Computer Science 
  47  *  Carnegie Mellon University 
  48  *  Pittsburgh PA 15213-3890 
  50  * any improvements or extensions that they make and grant Carnegie Mellon 
  51  * the rights to redistribute these changes. 
  57  *      processor.h:    Processor and processor-set definitions. 
  60 #ifndef _KERN_PROCESSOR_H_ 
  61 #define _KERN_PROCESSOR_H_ 
  64  *      Data structures for managing processors and sets of processors. 
  66 #include <mach/boolean.h> 
  67 #include <mach/kern_return.h> 
  68 #include <kern/kern_types.h> 
  70 #include <sys/appleapiopts.h> 
  72 #ifdef  __APPLE_API_PRIVATE 
  74 #ifdef  MACH_KERNEL_PRIVATE 
  78 #include <mach/mach_types.h> 
  79 #include <kern/cpu_number.h> 
  80 #include <kern/lock.h> 
  81 #include <kern/queue.h> 
  82 #include <kern/sched.h> 
  83 #include <kern/cpu_data.h> 
  85 #include <machine/ast_types.h> 
  87 struct processor_set 
{ 
  88         queue_head_t            idle_queue
;             /* idle processors */ 
  89         int                                     idle_count
;             /* how many ? */ 
  90         queue_head_t            active_queue
;   /* active processors */ 
  91         decl_simple_lock_data(,sched_lock
)      /* lock for above */ 
  93         queue_head_t            processors
;             /* all processors here */ 
  94         int                                     processor_count
;/* how many ? */ 
  95         decl_simple_lock_data(,processors_lock
) /* lock for above */ 
  97         struct  run_queue       runq
;                   /* runq for this set */ 
  99         queue_head_t            tasks
;                  /* tasks assigned */ 
 100         int                                     task_count
;             /* how many */ 
 101         queue_head_t            threads
;                /* threads in this set */ 
 102         int                                     thread_count
;   /* how many */ 
 103         int                                     ref_count
;              /* structure ref count */ 
 104         boolean_t                       active
;                 /* is pset in use */ 
 105         decl_mutex_data(,       lock
)                   /* lock for above */ 
 107         int                                     set_quanta
;             /* timeslice quanta for timesharing */ 
 108         int                                     machine_quanta
[NCPUS
+1]; 
 110         struct ipc_port 
*       pset_self
;              /* port for operations */ 
 111         struct ipc_port 
*       pset_name_self
; /* port for information */ 
 113         uint32_t                        run_count
;              /* number of threads running in set */ 
 115         integer_t                       mach_factor
;    /* mach_factor */ 
 116         integer_t                       load_average
;   /* load_average */ 
 117         uint32_t                        sched_load
;             /* load avg for scheduler */ 
 121         queue_chain_t           processor_queue
;/* idle/active/action queue link, 
 122                                                                                  * MUST remain the first element */ 
 123         int                                     state
;                  /* See below */ 
 124         int                                     current_pri
;    /* priority of current thread */ 
 125         struct thread_shuttle
 
 126                                                 *next_thread
,   /* next thread to run if dispatched */ 
 127                                                 *idle_thread
;   /* this processor's idle thread. */ 
 128         timer_call_data_t       quantum_timer
;  /* timer for quantum expiration */ 
 129         int                                     slice_quanta
;   /* quanta before timeslice ends */ 
 130         uint64_t                        quantum_end
;    /* time when current quantum ends */ 
 131         uint64_t                        last_dispatch
;  /* time of last dispatch */ 
 133         struct run_queue        runq
;                   /* local runq for this processor */ 
 135         processor_set_t         processor_set
;          /* current membership */ 
 136         processor_set_t         processor_set_next
;     /* set to join in progress */ 
 137         queue_chain_t           processors
;                     /* all processors in set */ 
 138         decl_simple_lock_data(,lock
) 
 139         struct ipc_port         
*processor_self
;/* port for operations */ 
 140         cpu_data_t                      
*cpu_data
;              /* machine-dep per-cpu data */ 
 141         int                                     slot_num
;               /* machine-indep slot number */ 
 144 extern struct processor_set     default_pset
; 
 145 extern processor_t      master_processor
; 
 147 extern struct processor processor_array
[NCPUS
]; 
 150  *      NOTE: The processor->processor_set link is needed in one of the 
 151  *      scheduler's critical paths.  [Figure out where to look for another 
 152  *      thread to run on this processor.]  It is accessed without locking. 
 153  *      The following access protocol controls this field. 
 155  *      Read from own processor - just read. 
 156  *      Read from another processor - lock processor structure during read. 
 157  *      Write from own processor - lock processor structure during write. 
 158  *      Write from another processor - NOT PERMITTED. 
 163  *      Processor state locking: 
 165  *      Values for the processor state are defined below.  If the processor 
 166  *      is off-line or being shutdown, then it is only necessary to lock 
 167  *      the processor to change its state.  Otherwise it is only necessary 
 168  *      to lock its processor set's sched_lock.  Scheduler code will 
 169  *      typically lock only the sched_lock, but processor manipulation code 
 170  *      will often lock both. 
 173 #define PROCESSOR_OFF_LINE              0       /* Not in system */ 
 174 #define PROCESSOR_RUNNING               1       /* Running a normal thread */ 
 175 #define PROCESSOR_IDLE                  2       /* idle */ 
 176 #define PROCESSOR_DISPATCHING   3       /* dispatching (idle -> running) */ 
 177 #define PROCESSOR_ASSIGN                4       /* Assignment is changing */ 
 178 #define PROCESSOR_SHUTDOWN              5       /* Being shutdown */ 
 179 #define PROCESSOR_START                 6       /* Being start */ 
 182  *      Use processor ptr array to find current processor's data structure. 
 183  *      This replaces a multiplication (index into processor_array) with 
 184  *      an array lookup and a memory reference.  It also allows us to save 
 185  *      space if processor numbering gets too sparse. 
 188 extern processor_t      processor_ptr
[NCPUS
]; 
 190 #define cpu_to_processor(i)     (processor_ptr[i]) 
 192 #define current_processor()     (processor_ptr[cpu_number()]) 
 193 #define current_processor_set() (current_processor()->processor_set) 
 195 /* Compatibility -- will go away */ 
 197 #define cpu_state(slot_num)     (processor_ptr[slot_num]->state) 
 198 #define cpu_idle(slot_num)      (cpu_state(slot_num) == PROCESSOR_IDLE) 
 200 /* Useful lock macros */ 
 202 #define pset_lock(pset)         mutex_lock(&(pset)->lock) 
 203 #define pset_lock_try(pset)     mutex_try(&(pset)->lock) 
 204 #define pset_unlock(pset)       mutex_unlock(&(pset)->lock) 
 206 #define processor_lock(pr)      simple_lock(&(pr)->lock) 
 207 #define processor_unlock(pr)    simple_unlock(&(pr)->lock) 
 209 extern void             pset_sys_bootstrap(void); 
 211 #define pset_quanta_update(pset)                                                \ 
 213         int             proc_count = (pset)->processor_count;           \ 
 214         int             runq_count = (pset)->runq.count;                        \ 
 216         (pset)->set_quanta = (pset)->machine_quanta[            \ 
 217                                         (runq_count > proc_count)?                      \ 
 218                                                         proc_count: runq_count];        \ 
 221 /* Implemented by MD layer */ 
 226 extern kern_return_t    
processor_shutdown( 
 227                                 processor_t     processor
); 
 229 extern void             pset_remove_processor( 
 230                                 processor_set_t pset
, 
 231                                 processor_t     processor
); 
 233 extern void             pset_add_processor( 
 234                                 processor_set_t pset
, 
 235                                 processor_t     processor
); 
 237 extern void             pset_remove_task( 
 238                                 processor_set_t pset
, 
 241 extern void             pset_add_task( 
 242                                 processor_set_t pset
, 
 245 extern void             pset_remove_thread( 
 246                                 processor_set_t pset
, 
 249 extern void             pset_add_thread( 
 250                                 processor_set_t pset
, 
 253 extern void             thread_change_psets( 
 255                                 processor_set_t old_pset
, 
 256                                 processor_set_t new_pset
); 
 258 extern kern_return_t    
processor_assign( 
 259                                 processor_t     processor
, 
 260                                 processor_set_t new_pset
, 
 263 extern kern_return_t    
processor_info_count( 
 264                                 processor_flavor_t flavor
, 
 265                                 mach_msg_type_number_t 
*count
); 
 267 #endif  /* MACH_KERNEL_PRIVATE */ 
 269 extern kern_return_t    
processor_start( 
 270                                 processor_t     processor
); 
 272 extern kern_return_t    
processor_exit( 
 273                                 processor_t     processor
); 
 275 #endif  /* __APPLE_API_PRIVATE */ 
 277 extern void             pset_deallocate( 
 278                                 processor_set_t pset
); 
 280 extern void             pset_reference( 
 281                                 processor_set_t pset
); 
 283 #endif  /* _KERN_PROCESSOR_H_ */