3 * \brief Code responsible for booting application cores
7 * Copyright (c) 2007, 2008, 2009, 2010, ETH Zurich.
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
20 #include <barrelfish/barrelfish.h>
21 #include <barrelfish/cpu_arch.h>
23 #include <if/monitor_defs.h>
25 #include <octopus/octopus.h>
27 #include <trace/trace.h>
28 #include <barrelfish/spawn_client.h>
30 #include <hw_records.h>
34 static const char *processor_regex = HW_PROCESSOR_GENERIC_REGEX;
36 static void cpu_change_event(octopus_mode_t mode, char* record, void* state)
38 if (mode & OCT_ON_SET) {
39 KALUGA_DEBUG("CPU found: %s\n", record);
41 /* try to extract basic information from the record */
42 uint64_t barrelfish_id, type, hw_id, enabled = 0;
43 errval_t err = oct_read(record, "_ { " HW_PROCESSOR_GENERIC_FIELDS " }",
44 &enabled, &barrelfish_id, &hw_id, &type);
45 if (err_is_fail(err)) {
46 DEBUG_ERR(err, "Cannot read record.");
47 printf("Malformed CPU record. Do not boot discovered CPU %"PRIu64".\n",
52 /* find the corectrl module for the given cpu type */
53 struct module_info* mi = find_corectrl_for_cpu_type((enum cpu_type)type);
55 err = mi->start_function(0, mi, record);
56 if (err_is_fail(err)) {
57 printf("Boot driver not found. Do not boot discovered CPU %"PRIu64".\n",
63 if (mode & OCT_ON_DEL) {
64 KALUGA_DEBUG("CPU removed: %s\n", record);
69 assert(!(mode & OCT_REMOVED));
72 errval_t watch_for_cores(void)
74 octopus_trigger_id_t tid;
75 return oct_trigger_existing_and_watch(processor_regex, cpu_change_event,
79 errval_t start_boot_driver(coreid_t where, struct module_info* mi,
85 if (!is_auto_driver(mi)) {
86 return KALUGA_ERR_DRIVER_NOT_AUTO;
89 // Construct additional command line arguments containing pci-id.
90 // We need one extra entry for the new argument.
91 char **argv = mi->argv;
93 char barrelfish_id_s[10];
94 size_t argc = mi->argc;
96 KALUGA_DEBUG("Starting corectrl for %s\n", record);
97 uint64_t barrelfish_id, cpu_type, hw_id, enabled = 0;
98 err = oct_read(record, "_ { " HW_PROCESSOR_GENERIC_FIELDS " }",
99 &enabled, &barrelfish_id, &hw_id, &cpu_type);
100 if (err_is_ok(err)) {
102 * XXX: change this to a generic cpuhwid instead of apic!
104 skb_add_fact("corename(%"PRIu64", %s, apic(%"PRIu64")).",
105 barrelfish_id, cpu_type_to_archstr(cpu_type), hw_id);
107 /* we are already running */
108 if (barrelfish_id == my_core_id) {
113 printf("CPU %" PRIu64 " is not enabled. Skipping driver initialization\n",
118 argv = malloc((argc+5) * sizeof(char *));
119 memcpy(argv, mi->argv, argc * sizeof(char *));
120 snprintf(barrelfish_id_s, 10, "%"PRIu64"", barrelfish_id);
124 argv[argc] = barrelfish_id_s;
126 // Copy kernel args over to new core
127 struct module_info* cpu_module = find_module("cpu");
128 if (cpu_module != NULL && strlen(cpu_module->args) > 1) {
129 KALUGA_DEBUG("%s:%s:%d: Boot with cpu arg %s and barrelfish_id_s=%s\n",
130 __FILE__, __FUNCTION__, __LINE__, cpu_module->args, barrelfish_id_s);
133 argv[argc] = cpu_module->args;
141 DEBUG_ERR(err, "Malformed CPU record?");
145 struct capref task_cap_kernel;
146 task_cap_kernel.cnode = cnode_task;
147 task_cap_kernel.slot = TASKCN_SLOT_KERNELCAP;
149 #ifdef KALUGA_SERVICE_DEBUG
150 struct capability info;
151 err = debug_cap_identify(task_cap_kernel, &info);
152 if (err_is_fail(err)) {
153 USER_PANIC_ERR(err, "Can not identify the capability.");
156 debug_print_cap(buffer, 1024, &info);
157 KALUGA_DEBUG("%s:%d: capability=%s\n", __FILE__, __LINE__, buffer);
160 struct capref inheritcn_cap;
161 err = alloc_inheritcn_with_caps(&inheritcn_cap,
162 NULL_CAP, NULL_CAP, task_cap_kernel);
163 if (err_is_fail(err)) {
164 DEBUG_ERR(err, "alloc_inheritcn_with_caps failed.");
167 err = spawn_program_with_caps(where, mi->path, argv,
168 environ, inheritcn_cap,
169 NULL_CAP, SPAWN_FLAGS_NEW_DOMAIN,
171 if (err_is_fail(err)) {
172 DEBUG_ERR(err, "Spawning %s failed.", mi->path);
183 static void spawnd_change_event(octopus_mode_t mode, char* record, void* state)
185 size_t count = (size_t) state;
186 static coreid_t spawnd_counter = 0;
188 if (mode & OCT_ON_SET) {
189 KALUGA_DEBUG("spawnd found: %s\n", record);
192 if (spawnd_counter == count) {
193 KALUGA_DEBUG("Found enough spawnds, setting all_spawnds_up\n");
194 errval_t err = oct_set("all_spawnds_up { iref: 0 }");
195 assert(err_is_ok(err));
200 errval_t wait_for_all_spawnds(void)
202 // Note: The whole wait for all_spawnds_up thing is a hack.
203 // Our overall design goal is a system where cores
204 // come and go dynamically and we do not want / need
205 // to wait for a stable state.
206 // However, some of our code (for example domain spanning)
207 // still assumes a fixed set of cores and will deadlock
208 // otherwise. Therefore we need to fix those parts first.
209 KALUGA_DEBUG("Waiting for acpi");
211 errval_t err = oct_wait_for(&record, "acpi { iref: _ }");
212 if (err_is_fail(err)) {
213 return err_push(err, KALUGA_ERR_WAITING_FOR_ACPI);
216 // No we should be able to get core count
217 // of all cores to estimate the amount of
218 // spawnd's we have to expect (one per core)
221 err = oct_get_names(&names, &count, processor_regex);
222 if (err_is_fail(err)) {
223 return err_push(err, KALUGA_ERR_QUERY_LOCAL_APIC);
225 oct_free_names(names, count);
227 static char* spawnds = "r'spawn.[0-9]+' { iref: _ }";
228 octopus_trigger_id_t tid;
229 return oct_trigger_existing_and_watch(spawnds, spawnd_change_event, (void*)count, &tid);