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