2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 #include <vm/vm_kern.h>
23 #include <mach/machine.h>
24 #include <i386/cpu_threads.h>
25 #include <i386/cpuid.h>
26 #include <i386/machine_cpu.h>
27 #include <i386/lock.h>
28 #include <i386/perfmon.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.
39 cpu_is_hyperthreaded(void)
41 if (cpuid_features() & CPUID_FEATURE_HTT
)
42 return (cpuid_info()->cpuid_logical_per_package
/
43 cpuid_info()->cpuid_cores_per_package
) > 1;
49 cpu_thread_alloc(int cpu
)
56 * Assume that all cpus have the same features.
58 if (cpu_is_hyperthreaded()) {
60 * Get the cpu number of the base thread in the core.
62 core_base_cpu
= cpu_to_core_cpu(cpu
);
63 cpu_datap(cpu
)->cpu_threadtype
= CPU_THREADTYPE_INTEL_HTT
;
66 cpu_datap(cpu
)->cpu_threadtype
= CPU_THREADTYPE_NONE
;
69 core
= (cpu_core_t
*) cpu_to_core(core_base_cpu
);
71 ret
= kmem_alloc(kernel_map
,
72 (void *) &core
, sizeof(cpu_core_t
));
73 if (ret
!= KERN_SUCCESS
)
74 panic("cpu_thread_alloc() kmem_alloc ret=%d\n", ret
);
75 bzero((void *) core
, sizeof(cpu_core_t
));
77 core
->base_cpu
= core_base_cpu
;
79 atomic_incl((long *) &machine_info
.physical_cpu_max
, 1);
81 /* Allocate performance counter data area (if available) */
82 core
->pmc
= pmc_alloc();
84 atomic_incl((long *) &machine_info
.logical_cpu_max
, 1);
92 int my_cpu
= get_cpu_number();
96 * If we're the boot processor we allocate the core structure here.
97 * Otherwise the core has already been allocated (by the boot cpu).
99 if (my_cpu
== master_cpu
)
100 cpu_to_core(master_cpu
) = cpu_thread_alloc(master_cpu
);
102 my_core
= cpu_core();
104 panic("cpu_thread_init() no core allocated for cpu %d", my_cpu
);
106 atomic_incl((long *) &my_core
->active_threads
, 1);
107 atomic_incl((long *) &machine_info
.logical_cpu
, 1);
108 /* Note: cpus are started serially so this isn't as racey as it looks */
109 if (my_core
->num_threads
== 0)
110 atomic_incl((long *) &machine_info
.physical_cpu
, 1);
111 atomic_incl((long *) &my_core
->num_threads
, 1);
115 * Called for a cpu to halt permanently
116 * (as opposed to halting and expecting an interrupt to awaken it).
119 cpu_thread_halt(void)
121 cpu_core_t
*my_core
= cpu_core();
123 atomic_decl((long *) &machine_info
.logical_cpu
, 1);
124 atomic_decl((long *) &my_core
->active_threads
, 1);
125 if (atomic_decl_and_test((long *) &my_core
->num_threads
, 1))
126 atomic_decl((long *) &machine_info
.physical_cpu
, 1);