]> git.saurik.com Git - apple/libc.git/blob - x86_64/gen/cpu_number.s
e76be3d93881d5d7efc4ef5ebcb1490f15b72669
[apple/libc.git] / x86_64 / gen / cpu_number.s
1 /*
2 * Copyright (c) 2008 Apple 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
29 /*
30 * This routine provides fast access to the logical cpu number
31 * of the calling processor assuming no pre-emption occurs. This number
32 * is encoded in the bottom 12-bits of the limit field of the IDTR (the
33 * Interrupt Descriptor Table Register). The SIDT instruction is used in
34 * userspace to read this register and thus to gain access to the cpu number.
35 * The IDTR is loaded by the kernel for each processor at startup - see
36 * osfmk/i386/mp_desc.c.
37 */
38
39 /* return logical cpu number in %rax */
40
41 .private_extern _cpu_number
42 _cpu_number:
43 push %rbp
44 mov %rsp,%rbp
45 sub $16,%rsp // space to read IDTR
46
47 sidt (%rsp) // store limit:base on stack
48 movw (%rsp), %ax // get limit
49 and $0xfff, %rax // mask off lower 12 bits to return
50
51 mov %rbp,%rsp
52 pop %rbp
53 ret