1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2025 Ant Group
* Author: Tiwei Bie <tiwei.btw@antgroup.com>
*
* Based on the previous implementation in TT mode
* Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
*/
#include <linux/sched.h>
#include <linux/sched/task.h>
#include <linux/sched/task_stack.h>
#include <linux/module.h>
#include <linux/processor.h>
#include <linux/threads.h>
#include <linux/cpu.h>
#include <linux/hardirq.h>
#include <linux/smp.h>
#include <linux/smp-internal.h>
#include <init.h>
#include <kern.h>
#include <os.h>
#include <smp.h>
enum {
UML_IPI_RES = 0,
UML_IPI_CALL_SINGLE,
UML_IPI_CALL,
UML_IPI_STOP,
};
void arch_smp_send_reschedule(int cpu)
{
os_send_ipi(cpu, UML_IPI_RES);
}
void arch_send_call_function_single_ipi(int cpu)
{
os_send_ipi(cpu, UML_IPI_CALL_SINGLE);
}
void arch_send_call_function_ipi_mask(const struct cpumask *mask)
{
int cpu;
for_each_cpu(cpu, mask)
os_send_ipi(cpu, UML_IPI_CALL);
}
void smp_send_stop(void)
{
int cpu, me = smp_processor_id();
for_each_online_cpu(cpu) {
if (cpu == me)
continue;
os_send_ipi(cpu, UML_IPI_STOP);
}
}
static void ipi_handler(int vector, struct uml_pt_regs *regs)
{
struct pt_regs *old_regs = set_irq_regs((struct pt_regs *)regs);
int cpu = raw_smp_processor_id();
irq_enter();
if (current->mm)
os_alarm_process(current->mm->context.id.pid);
switch (vector) {
case UML_IPI_RES:
inc_irq_stat(irq_resched_count);
scheduler_ipi();
break;
case UML_IPI_CALL_SINGLE:
inc_irq_stat(irq_call_count);
generic_smp_call_function_single_interrupt();
break;
case UML_IPI_CALL:
inc_irq_stat(irq_call_count);
generic_smp_call_function_interrupt();
break;
case UML_IPI_STOP:
set_cpu_online(cpu, false);
while (1)
pause();
break;
default:
pr_err("CPU#%d received unknown IPI (vector=%d)!\n", cpu, vector);
break;
}
irq_exit();
set_irq_regs(old_regs);
}
void uml_ipi_handler(int vector)
{
struct uml_pt_regs r = { .is_user = 0 };
preempt_disable();
ipi_handler(vector, &r);
preempt_enable();
}
/* AP states used only during CPU startup */
enum {
UML_CPU_PAUSED = 0,
UML_CPU_RUNNING,
};
static int cpu_states[NR_CPUS];
static int start_secondary(void *unused)
{
int err, cpu = raw_smp_processor_id();
notify_cpu_starting(cpu);
set_cpu_online(cpu, true);
err = um_setup_timer();
if (err)
panic("CPU#%d failed to setup timer, err = %d", cpu, err);
local_irq_enable();
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
return 0;
}
void uml_start_secondary(void *opaque)
{
int cpu = raw_smp_processor_id();
struct mm_struct *mm = &init_mm;
struct task_struct *idle;
stack_protections((unsigned long) &cpu_irqstacks[cpu]);
set_sigstack(&cpu_irqstacks[cpu], THREAD_SIZE);
set_cpu_present(cpu, true);
os_futex_wait(&cpu_states[cpu], UML_CPU_PAUSED);
smp_rmb(); /* paired with smp_wmb() in __cpu_up() */
idle = cpu_tasks[cpu];
idle->thread_info.cpu = cpu;
mmgrab(mm);
idle->active_mm = mm;
idle->thread.request.thread.proc = start_secondary;
idle->thread.request.thread.arg = NULL;
new_thread(task_stack_page(idle), &idle->thread.switch_buf,
new_thread_handler);
os_start_secondary(opaque, &idle->thread.switch_buf);
}
void __init smp_prepare_cpus(unsigned int max_cpus)
{
int err, cpu, me = smp_processor_id();
unsigned long deadline;
os_init_smp();
for_each_possible_cpu(cpu) {
if (cpu == me)
continue;
pr_debug("Booting processor %d...\n", cpu);
err = os_start_cpu_thread(cpu);
if (err) {
pr_crit("CPU#%d failed to start cpu thread, err = %d",
cpu, err);
continue;
}
deadline = jiffies + msecs_to_jiffies(1000);
spin_until_cond(cpu_present(cpu) ||
time_is_before_jiffies(deadline));
if (!cpu_present(cpu))
pr_crit("CPU#%d failed to boot\n", cpu);
}
}
int __cpu_up(unsigned int cpu, struct task_struct *tidle)
{
cpu_tasks[cpu] = tidle;
smp_wmb(); /* paired with smp_rmb() in uml_start_secondary() */
cpu_states[cpu] = UML_CPU_RUNNING;
os_futex_wake(&cpu_states[cpu]);
spin_until_cond(cpu_online(cpu));
return 0;
}
void __init smp_cpus_done(unsigned int max_cpus)
{
}
/* Set in uml_ncpus_setup */
int uml_ncpus = 1;
void __init prefill_possible_map(void)
{
int cpu;
for (cpu = 0; cpu < uml_ncpus; cpu++)
set_cpu_possible(cpu, true);
for (; cpu < NR_CPUS; cpu++)
set_cpu_possible(cpu, false);
}
static int __init uml_ncpus_setup(char *line, int *add)
{
*add = 0;
if (kstrtoint(line, 10, ¨_ncpus)) {
os_warn("%s: Couldn't parse '%s'\n", __func__, line);
return -1;
}
uml_ncpus = clamp(uml_ncpus, 1, NR_CPUS);
return 0;
}
__uml_setup("ncpus=", uml_ncpus_setup,
"ncpus=<# of desired CPUs>\n"
" This tells UML how many virtual processors to start. The maximum\n"
" number of supported virtual processors can be obtained by querying\n"
" the CONFIG_NR_CPUS option using --showconfig.\n\n"
);
EXPORT_SYMBOL(uml_curr_cpu);
|