Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core/shmem): move shared memory logic to dedicated source file #124

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion src/core/inc/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ struct vm_config;

long int ipc_hypercall(unsigned long arg0, unsigned long arg1, unsigned long arg2);
void ipc_init(void);
struct shmem* ipc_get_shmem(size_t shmem_id);

#endif /* IPC_H */
14 changes: 14 additions & 0 deletions src/core/inc/shmem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* SPDX-License-Identifier: Apache-2.0
* Copyright (c) Bao Project and Contributors. All rights reserved.
*/

#ifndef SHMEM_H
#define SHMEM_H

#include <mem.h>

void shmem_init(void);
struct shmem* shmem_get(size_t shmem_id);

#endif /* SHMEM_H */
43 changes: 2 additions & 41 deletions src/core/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <vmm.h>
#include <hypercall.h>
#include <config.h>
#include <shmem.h>

enum { IPC_NOTIFY };

Expand All @@ -20,18 +21,6 @@ union ipc_msg_data {
uint64_t raw;
};

static size_t shmem_table_size;
static struct shmem* shmem_table;

struct shmem* ipc_get_shmem(size_t shmem_id)
{
if (shmem_id < shmem_table_size) {
return &shmem_table[shmem_id];
} else {
return NULL;
}
}

static struct ipc* ipc_find_by_shmemid(struct vm* vm, size_t shmem_id)
{
struct ipc* ipc_obj = NULL;
Expand Down Expand Up @@ -78,7 +67,7 @@ long int ipc_hypercall(unsigned long ipc_id, unsigned long ipc_event, unsigned l
struct shmem* shmem = NULL;
bool valid_ipc_obj = ipc_id < cpu()->vcpu->vm->ipc_num;
if (valid_ipc_obj) {
shmem = ipc_get_shmem(cpu()->vcpu->vm->ipcs[ipc_id].shmem_id);
shmem = shmem_get(cpu()->vcpu->vm->ipcs[ipc_id].shmem_id);
}
bool valid_shmem = shmem != NULL;

Expand All @@ -103,31 +92,3 @@ long int ipc_hypercall(unsigned long ipc_id, unsigned long ipc_event, unsigned l

return ret;
}

static void ipc_alloc_shmem()
{
for (size_t i = 0; i < shmem_table_size; i++) {
struct shmem* shmem = &shmem_table[i];
if (!shmem->place_phys) {
size_t n_pg = NUM_PAGES(shmem->size);
struct ppages ppages = mem_alloc_ppages(shmem->colors, n_pg, false);
if (ppages.num_pages < n_pg) {
ERROR("failed to allocate shared memory");
}
shmem->phys = ppages.base;
}
}
}

void ipc_init(void)
{
if (cpu_is_master()) {
shmem_table_size = config.shmemlist_size;
shmem_table = config.shmemlist;
ipc_alloc_shmem();

for (size_t i = 0; i < config.shmemlist_size; i++) {
config.shmemlist[i].cpu_masters = 0;
}
}
}
1 change: 1 addition & 0 deletions src/core/objects.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ core-objs-y+=console.o
core-objs-y+=ipc.o
core-objs-y+=objpool.o
core-objs-y+=hypercall.o
core-objs-y+=shmem.o
47 changes: 47 additions & 0 deletions src/core/shmem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* SPDX-License-Identifier: Apache-2.0
* Copyright (c) Bao Project and Contributors. All rights reserved.
*/

#include <shmem.h>
#include <config.h>

static size_t shmem_table_size;
static struct shmem* shmem_table;

static void shmem_alloc()
{
for (size_t i = 0; i < shmem_table_size; i++) {
struct shmem* shmem = &shmem_table[i];
if (!shmem->place_phys) {
size_t n_pg = NUM_PAGES(shmem->size);
struct ppages ppages = mem_alloc_ppages(shmem->colors, n_pg, false);
if (ppages.num_pages < n_pg) {
ERROR("failed to allocate shared memory");
}
shmem->phys = ppages.base;
}
}
}

struct shmem* shmem_get(size_t shmem_id)
{
if (shmem_id < shmem_table_size) {
return &shmem_table[shmem_id];
} else {
return NULL;
}
}

void shmem_init()
{
if (cpu_is_master()) {
shmem_table_size = config.shmemlist_size;
shmem_table = config.shmemlist;
shmem_alloc();

for (size_t i = 0; i < config.shmemlist_size; i++) {
config.shmemlist[i].cpu_masters = 0;
}
}
}
3 changes: 2 additions & 1 deletion src/core/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <mem.h>
#include <cache.h>
#include <config.h>
#include <shmem.h>

static void vm_master_init(struct vm* vm, const struct vm_config* vm_config, vmid_t vm_id)
{
Expand Down Expand Up @@ -166,7 +167,7 @@ static void vm_init_ipc(struct vm* vm, const struct vm_config* vm_config)
vm->ipcs = vm_config->platform.ipcs;
for (size_t i = 0; i < vm_config->platform.ipc_num; i++) {
struct ipc* ipc = &vm_config->platform.ipcs[i];
struct shmem* shmem = ipc_get_shmem(ipc->shmem_id);
struct shmem* shmem = shmem_get(ipc->shmem_id);
if (shmem == NULL) {
WARNING("Invalid shmem id in configuration. Ignored.");
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/core/vmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <spinlock.h>
#include <fences.h>
#include <string.h>
#include <ipc.h>
#include <shmem.h>

static struct vm_assignment {
spinlock_t lock;
Expand Down Expand Up @@ -127,7 +127,7 @@ void vmm_init()
{
vmm_arch_init();
vmm_io_init();
ipc_init();
shmem_init();

cpu_sync_barrier(&cpu_glb_sync);

Expand Down