]> git.cameronkatri.com Git - apple_cmds.git/blob - system_cmds/cpuctl.tproj/cpuctl.c
md5: Don't symlink non working bins, setuid appropriate bins
[apple_cmds.git] / system_cmds / cpuctl.tproj / cpuctl.c
1 //
2 // cpuctl.c
3 // system_cmds
4 //
5 // Copyright (c) 2019 Apple Inc. All rights reserved.
6 //
7
8 #include <err.h>
9 #include <getopt.h>
10 #include <limits.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sysexits.h>
16 #include <unistd.h>
17 typedef char *kobject_description_t[512];
18 #include <mach/mach.h>
19
20 static void usage()
21 {
22 printf("usage: cpuctl [ list ]\n");
23 printf(" cpuctl { offline | online } <cpu> [ <cpu>... ]\n");
24 exit(EX_USAGE);
25 }
26
27 static void fetch_cpu_info(host_t *priv_port,
28 processor_port_array_t proc_ports,
29 mach_msg_type_number_t proc_count,
30 processor_basic_info_data_t *cpus)
31 {
32 for (int i = 0; i < proc_count; i++) {
33 mach_msg_type_number_t info_count = PROCESSOR_BASIC_INFO_COUNT;
34
35 if (processor_info(proc_ports[i], PROCESSOR_BASIC_INFO, priv_port,
36 (processor_info_t)&cpus[i], &info_count) != KERN_SUCCESS) {
37 errx(EX_OSERR, "processor_info(%d) failed", i);
38 }
39 }
40 }
41
42 static int do_cmd_list(mach_msg_type_number_t proc_count, processor_basic_info_data_t *cpus)
43 {
44 int prev_lowest = -1;
45 for (int i = 0; i < proc_count; i++) {
46 int lowest_slot = INT_MAX;
47 int lowest_idx = -1;
48 for (int j = 0; j < proc_count; j++) {
49 int slot = cpus[j].slot_num;
50 if (slot > prev_lowest && slot < lowest_slot) {
51 lowest_slot = slot;
52 lowest_idx = j;
53 }
54 }
55 if (lowest_idx == -1)
56 errx(EX_OSERR, "slot numbers are out of range");
57
58 processor_basic_info_data_t *cpu = &cpus[lowest_idx];
59 printf("CPU%d: %-7s type=%x,%x master=%d\n",
60 cpu->slot_num,
61 cpu->running ? "online" : "offline",
62 cpu->cpu_type,
63 cpu->cpu_subtype,
64 cpu->is_master);
65
66 prev_lowest = lowest_slot;
67 }
68 return 0;
69 }
70
71 static int find_cpu_by_slot(mach_msg_type_number_t proc_count,
72 processor_basic_info_data_t *cpus,
73 int slot)
74 {
75 for (int i = 0; i < proc_count; i++) {
76 if (cpus[i].slot_num == slot)
77 return i;
78 }
79 return -1;
80 }
81
82 int main(int argc, char **argv)
83 {
84 int opt;
85
86 while ((opt = getopt(argc, argv, "h")) != -1) {
87 switch (opt) {
88 case 'h':
89 usage();
90 }
91 }
92
93 host_t priv_port;
94 if (host_get_host_priv_port(mach_host_self(), &priv_port) != KERN_SUCCESS)
95 errx(EX_OSERR, "host_get_host_priv_port() failed");
96
97 processor_port_array_t proc_ports;
98 mach_msg_type_number_t proc_count;
99 if (host_processors(priv_port, &proc_ports, &proc_count) != KERN_SUCCESS)
100 errx(EX_OSERR, "host_processors() failed");
101
102 processor_basic_info_data_t *cpus = calloc(proc_count, sizeof(*cpus));
103 if (!cpus)
104 errx(EX_OSERR, "calloc() failed");
105 fetch_cpu_info(&priv_port, proc_ports, proc_count, cpus);
106
107 if (optind == argc)
108 return do_cmd_list(proc_count, cpus);
109
110 const char *cmd = argv[optind];
111 optind++;
112
113 if (!strcmp(cmd, "list"))
114 return do_cmd_list(proc_count, cpus);
115
116 bool up = true;
117 if (!strncmp(cmd, "off", 3))
118 up = false;
119 else if (strncmp(cmd, "on", 2))
120 usage();
121
122 if (optind == argc)
123 usage();
124
125 int ret = 0;
126 for (; optind < argc; optind++) {
127 char *endp = NULL;
128 int slot = (int)strtoul(argv[optind], &endp, 0);
129 if (*endp != 0)
130 usage();
131
132 int cpu = find_cpu_by_slot(proc_count, cpus, slot);
133 if (cpu == -1)
134 errx(EX_USAGE, "Invalid CPU ID %d", slot);
135
136 if (up) {
137 if (processor_start(proc_ports[cpu]) != KERN_SUCCESS)
138 errx(EX_OSERR, "processor_start(%u) failed", cpu);
139 } else {
140 if (processor_exit(proc_ports[cpu]) != KERN_SUCCESS)
141 errx(EX_OSERR, "processor_exit(%u) failed", cpu);
142 }
143 }
144
145 return ret;
146 }