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