Skip to content

Commit

Permalink
common: add an abstraction handling detected CPUs' metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Pawel Wieczorkiewicz <[email protected]>
  • Loading branch information
wipawel committed Jul 16, 2022
1 parent df3c102 commit dd72127
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
112 changes: 112 additions & 0 deletions common/cpu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright © 2022 Open Source Security, Inc.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <console.h>
#include <cpu.h>
#include <ktf.h>
#include <lib.h>
#include <list.h>
#include <spinlock.h>
#include <string.h>

#include <mm/slab.h>

static list_head_t cpus;
static unsigned int nr_cpus = 0;

static cpu_t bsp = {0};

static void init_cpu(cpu_t *cpu, unsigned int id, bool bsp, bool enabled) {
memset(cpu, 0, sizeof(*cpu));
cpu->id = id;
cpu->bsp = bsp;
cpu->enabled = enabled;
cpu->done = false;

cpu->percpu = get_percpu_page(id);
BUG_ON(!cpu->percpu);

cpu->lock = SPINLOCK_INIT;
}

cpu_t *init_cpus(void) {
printk("Initialize CPU structures\n");

list_init(&cpus);

init_cpu(&bsp, 0, true, true);
list_add(&bsp.list, &cpus);
nr_cpus = 1;

return &bsp;
}

cpu_t *add_cpu(unsigned int id, bool bsp, bool enabled) {
cpu_t *cpu = kzalloc(sizeof(*cpu));

if (!cpu)
return NULL;

init_cpu(cpu, id, bsp, enabled);

list_add(&cpu->list, &cpus);
nr_cpus++;

return cpu;
}

cpu_t *get_cpu(unsigned int id) {
cpu_t *cpu;

list_for_each_entry (cpu, &cpus, list) {
if (cpu->id == id)
return cpu;
}

return NULL;
}

unsigned int get_nr_cpus(void) { return nr_cpus; }

cpu_t *get_bsp_cpu(void) { return &bsp; }

void for_each_cpu(void (*func)(cpu_t *cpu)) {
cpu_t *cpu;

list_for_each_entry (cpu, &cpus, list)
func(cpu);
}

void wait_for_all_cpus(void) {
cpu_t *cpu, *safe;

do {
list_for_each_entry_safe (cpu, safe, &cpus, list) {
spin_lock(&cpu->lock);
if (cpu->done)
list_unlink(&cpu->list);
spin_unlock(&cpu->lock);
}
} while (!list_is_empty(&cpus));
}
56 changes: 56 additions & 0 deletions include/cpu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright © 2022 Open Source Security, Inc.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef KTF_CPU_H
#define KTF_CPU_H

#include <ktf.h>
#include <lib.h>
#include <list.h>
#include <percpu.h>
#include <spinlock.h>

struct cpu {
list_head_t list;

unsigned int id;
unsigned int bsp : 1, enabled : 1, done : 1;

percpu_t *percpu;

spinlock_t lock;
};
typedef struct cpu cpu_t;

/* External declarations */

extern cpu_t *init_cpus(void);
extern cpu_t *add_cpu(unsigned int id, bool bsp, bool enabled);
extern cpu_t *get_cpu(unsigned int id);
extern cpu_t *get_bsp_cpu(void);
extern unsigned int get_nr_cpus(void);
extern void for_each_cpu(void (*func)(cpu_t *cpu));
extern void wait_for_all_cpus(void);

#endif /* KTF_CPU_H */

0 comments on commit dd72127

Please sign in to comment.