]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/cpu_threads.c
xnu-792.12.6.tar.gz
[apple/xnu.git] / osfmk / i386 / cpu_threads.c
CommitLineData
91447636
A
1/*
2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
3 *
8ad349bb 4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
91447636 5 *
8ad349bb
A
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@
91447636
A
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 */
42int idlehalt = 1;
43
8ad349bb
A
44void
45cpu_thread_init(void)
c0fea474 46{
8ad349bb
A
47 int my_cpu = get_cpu_number();
48 int my_core_base_cpu;
c0fea474 49 int ret;
8ad349bb 50 cpu_core_t *my_core;
91447636 51
8ad349bb
A
52 /* Have we initialized already for this cpu? */
53 if (cpu_core())
54 return;
55
56 if (cpuid_features() & CPUID_FEATURE_HTT) {
91447636
A
57 /*
58 * Get the cpu number of the base thread in the core.
59 */
8ad349bb
A
60 my_core_base_cpu = cpu_to_core_cpu(my_cpu);
61 current_cpu_datap()->cpu_threadtype = CPU_THREADTYPE_INTEL_HTT;
91447636 62 } else {
8ad349bb
A
63 my_core_base_cpu = my_cpu;
64 current_cpu_datap()->cpu_threadtype = CPU_THREADTYPE_NONE;
91447636
A
65 }
66
8ad349bb
A
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
91447636 77 ret = kmem_alloc(kernel_map,
8ad349bb 78 (void *) &new_core, sizeof(cpu_core_t));
91447636 79 if (ret != KERN_SUCCESS)
8ad349bb
A
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);
91447636 92 }
c0fea474 93
8ad349bb 94 cpu_to_core(my_cpu) = (struct cpu_core *) my_core;
91447636
A
95
96 atomic_incl((long *) &my_core->active_threads, 1);
c0fea474 97 atomic_incl((long *) &my_core->num_threads, 1);
8ad349bb
A
98 atomic_incl((long *) &machine_info.logical_cpu, 1);
99 atomic_incl((long *) &machine_info.logical_cpu_max, 1);
100
91447636
A
101}
102
103/*
104 * Called for a cpu to halt permanently
105 * (as opposed to halting and expecting an interrupt to awaken it).
106 */
107void
108cpu_thread_halt(void)
109{
110 cpu_core_t *my_core = cpu_core();
111
8ad349bb 112 /* Note: don't ever decrement the number of physical processors */
c0fea474 113 atomic_decl((long *) &my_core->active_threads, 1);
8ad349bb
A
114 atomic_decl((long *) &my_core->num_threads, 1);
115 atomic_decl((long *) &machine_info.logical_cpu, 1);
91447636
A
116
117 cpu_halt();
118}