]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/cpu_threads.c
2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <vm/vm_kern.h>
24 #include <mach/machine.h>
25 #include <i386/cpu_threads.h>
26 #include <i386/cpuid.h>
27 #include <i386/machine_cpu.h>
28 #include <i386/lock.h>
31 * Kernel parameter determining whether threads are halted unconditionally
32 * in the idle state. This is the default behavior.
33 * See machine_idle() for use.
40 int my_cpu
= get_cpu_number();
45 /* Have we initialized already for this cpu? */
49 if (cpuid_features() & CPUID_FEATURE_HTT
) {
51 * Get the cpu number of the base thread in the core.
53 my_core_base_cpu
= cpu_to_core_cpu(my_cpu
);
54 current_cpu_datap()->cpu_threadtype
= CPU_THREADTYPE_INTEL_HTT
;
56 my_core_base_cpu
= my_cpu
;
57 current_cpu_datap()->cpu_threadtype
= CPU_THREADTYPE_NONE
;
61 * Allocate the base cpu_core struct if none exists.
62 * Since we could be racing with other threads in the same core,
63 * this needs care without using locks. We allocate a new core
64 * structure and assign it atomically, freeing it if we lost the race.
66 my_core
= (cpu_core_t
*) cpu_to_core(my_core_base_cpu
);
67 if (my_core
== NULL
) {
70 ret
= kmem_alloc(kernel_map
,
71 (void *) &new_core
, sizeof(cpu_core_t
));
72 if (ret
!= KERN_SUCCESS
)
73 panic("cpu_thread_init() kmem_alloc ret=%d\n", ret
);
74 bzero((void *) new_core
, sizeof(cpu_core_t
));
75 new_core
->base_cpu
= my_core_base_cpu
;
76 if (atomic_cmpxchg((uint32_t *) &cpu_to_core(my_core_base_cpu
),
77 0, (uint32_t) new_core
)) {
78 atomic_incl((long *) &machine_info
.physical_cpu
, 1);
79 atomic_incl((long *) &machine_info
.physical_cpu_max
, 1);
82 (vm_offset_t
)new_core
, sizeof(cpu_core_t
));
84 my_core
= (cpu_core_t
*) cpu_to_core(my_core_base_cpu
);
87 cpu_to_core(my_cpu
) = (struct cpu_core
*) my_core
;
89 atomic_incl((long *) &my_core
->active_threads
, 1);
90 atomic_incl((long *) &my_core
->num_threads
, 1);
91 atomic_incl((long *) &machine_info
.logical_cpu
, 1);
92 atomic_incl((long *) &machine_info
.logical_cpu_max
, 1);
97 * Called for a cpu to halt permanently
98 * (as opposed to halting and expecting an interrupt to awaken it).
101 cpu_thread_halt(void)
103 cpu_core_t
*my_core
= cpu_core();
105 /* Note: don't ever decrement the number of physical processors */
106 atomic_decl((long *) &my_core
->active_threads
, 1);
107 atomic_decl((long *) &my_core
->num_threads
, 1);
108 atomic_decl((long *) &machine_info
.logical_cpu
, 1);