]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/cpu_threads.c
c2249c52130cc9abc4403b7c5bbba78998bbe5eb
[apple/xnu.git] / osfmk / i386 / cpu_threads.c
1 /*
2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 #include <vm/vm_kern.h>
31 #include <mach/machine.h>
32 #include <i386/cpu_threads.h>
33 #include <i386/cpuid.h>
34 #include <i386/machine_cpu.h>
35 #include <i386/lock.h>
36
37 /*
38 * Kernel parameter determining whether threads are halted unconditionally
39 * in the idle state. This is the default behavior.
40 * See machine_idle() for use.
41 */
42 int idlehalt = 1;
43
44 void
45 cpu_thread_init(void)
46 {
47 int my_cpu = get_cpu_number();
48 int my_core_base_cpu;
49 int ret;
50 cpu_core_t *my_core;
51
52 /* Have we initialized already for this cpu? */
53 if (cpu_core())
54 return;
55
56 if (cpuid_features() & CPUID_FEATURE_HTT) {
57 /*
58 * Get the cpu number of the base thread in the core.
59 */
60 my_core_base_cpu = cpu_to_core_cpu(my_cpu);
61 current_cpu_datap()->cpu_threadtype = CPU_THREADTYPE_INTEL_HTT;
62 } else {
63 my_core_base_cpu = my_cpu;
64 current_cpu_datap()->cpu_threadtype = CPU_THREADTYPE_NONE;
65 }
66
67 /*
68 * Allocate the base cpu_core struct if none exists.
69 * Since we could be racing with other threads in the same core,
70 * this needs care without using locks. We allocate a new core
71 * structure and assign it atomically, freeing it if we lost the race.
72 */
73 my_core = (cpu_core_t *) cpu_to_core(my_core_base_cpu);
74 if (my_core == NULL) {
75 cpu_core_t *new_core;
76
77 ret = kmem_alloc(kernel_map,
78 (void *) &new_core, sizeof(cpu_core_t));
79 if (ret != KERN_SUCCESS)
80 panic("cpu_thread_init() kmem_alloc ret=%d\n", ret);
81 bzero((void *) new_core, sizeof(cpu_core_t));
82 new_core->base_cpu = my_core_base_cpu;
83 if (atomic_cmpxchg((uint32_t *) &cpu_to_core(my_core_base_cpu),
84 0, (uint32_t) new_core)) {
85 atomic_incl((long *) &machine_info.physical_cpu, 1);
86 atomic_incl((long *) &machine_info.physical_cpu_max, 1);
87 } else {
88 kmem_free(kernel_map,
89 (vm_offset_t)new_core, sizeof(cpu_core_t));
90 }
91 my_core = (cpu_core_t *) cpu_to_core(my_core_base_cpu);
92 }
93
94 cpu_to_core(my_cpu) = (struct cpu_core *) my_core;
95
96 atomic_incl((long *) &my_core->active_threads, 1);
97 atomic_incl((long *) &my_core->num_threads, 1);
98 atomic_incl((long *) &machine_info.logical_cpu, 1);
99 atomic_incl((long *) &machine_info.logical_cpu_max, 1);
100
101 }
102
103 /*
104 * Called for a cpu to halt permanently
105 * (as opposed to halting and expecting an interrupt to awaken it).
106 */
107 void
108 cpu_thread_halt(void)
109 {
110 cpu_core_t *my_core = cpu_core();
111
112 /* Note: don't ever decrement the number of physical processors */
113 atomic_decl((long *) &my_core->active_threads, 1);
114 atomic_decl((long *) &my_core->num_threads, 1);
115 atomic_decl((long *) &machine_info.logical_cpu, 1);
116
117 cpu_halt();
118 }