Skip to content

Commit 26c01a3

Browse files
joaopeixoto13josecm
authored andcommitted
feat(core/shmem): move shared memory logic to dedicated source file
This patch relocates the initialization and shared memory access steps from the hardcoded settings in the existing IPC file to a generic shared memory file. Previously, the code assumed the existence of only one object working with the shared memory (ipc). With this update, the system is now prepared for future versions where multiple objects can be added and working with the shared memory. Signed-off-by: joaopeixoto13 <[email protected]> Signed-off-by: Jose Martins <[email protected]>
1 parent ddd6b82 commit 26c01a3

File tree

7 files changed

+68
-45
lines changed

7 files changed

+68
-45
lines changed

src/core/inc/ipc.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ struct vm_config;
2121

2222
long int ipc_hypercall(unsigned long arg0, unsigned long arg1, unsigned long arg2);
2323
void ipc_init(void);
24-
struct shmem* ipc_get_shmem(size_t shmem_id);
2524

2625
#endif /* IPC_H */

src/core/inc/shmem.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright (c) Bao Project and Contributors. All rights reserved.
4+
*/
5+
6+
#ifndef SHMEM_H
7+
#define SHMEM_H
8+
9+
#include <mem.h>
10+
11+
void shmem_init(void);
12+
struct shmem* shmem_get(size_t shmem_id);
13+
14+
#endif /* SHMEM_H */

src/core/ipc.c

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <vmm.h>
1010
#include <hypercall.h>
1111
#include <config.h>
12+
#include <shmem.h>
1213

1314
enum { IPC_NOTIFY };
1415

@@ -20,18 +21,6 @@ union ipc_msg_data {
2021
uint64_t raw;
2122
};
2223

23-
static size_t shmem_table_size;
24-
static struct shmem* shmem_table;
25-
26-
struct shmem* ipc_get_shmem(size_t shmem_id)
27-
{
28-
if (shmem_id < shmem_table_size) {
29-
return &shmem_table[shmem_id];
30-
} else {
31-
return NULL;
32-
}
33-
}
34-
3524
static struct ipc* ipc_find_by_shmemid(struct vm* vm, size_t shmem_id)
3625
{
3726
struct ipc* ipc_obj = NULL;
@@ -78,7 +67,7 @@ long int ipc_hypercall(unsigned long ipc_id, unsigned long ipc_event, unsigned l
7867
struct shmem* shmem = NULL;
7968
bool valid_ipc_obj = ipc_id < cpu()->vcpu->vm->ipc_num;
8069
if (valid_ipc_obj) {
81-
shmem = ipc_get_shmem(cpu()->vcpu->vm->ipcs[ipc_id].shmem_id);
70+
shmem = shmem_get(cpu()->vcpu->vm->ipcs[ipc_id].shmem_id);
8271
}
8372
bool valid_shmem = shmem != NULL;
8473

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

10493
return ret;
10594
}
106-
107-
static void ipc_alloc_shmem()
108-
{
109-
for (size_t i = 0; i < shmem_table_size; i++) {
110-
struct shmem* shmem = &shmem_table[i];
111-
if (!shmem->place_phys) {
112-
size_t n_pg = NUM_PAGES(shmem->size);
113-
struct ppages ppages = mem_alloc_ppages(shmem->colors, n_pg, false);
114-
if (ppages.num_pages < n_pg) {
115-
ERROR("failed to allocate shared memory");
116-
}
117-
shmem->phys = ppages.base;
118-
}
119-
}
120-
}
121-
122-
void ipc_init(void)
123-
{
124-
if (cpu_is_master()) {
125-
shmem_table_size = config.shmemlist_size;
126-
shmem_table = config.shmemlist;
127-
ipc_alloc_shmem();
128-
129-
for (size_t i = 0; i < config.shmemlist_size; i++) {
130-
config.shmemlist[i].cpu_masters = 0;
131-
}
132-
}
133-
}

src/core/objects.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ core-objs-y+=console.o
1313
core-objs-y+=ipc.o
1414
core-objs-y+=objpool.o
1515
core-objs-y+=hypercall.o
16+
core-objs-y+=shmem.o

src/core/shmem.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright (c) Bao Project and Contributors. All rights reserved.
4+
*/
5+
6+
#include <shmem.h>
7+
#include <config.h>
8+
9+
static size_t shmem_table_size;
10+
static struct shmem* shmem_table;
11+
12+
static void shmem_alloc()
13+
{
14+
for (size_t i = 0; i < shmem_table_size; i++) {
15+
struct shmem* shmem = &shmem_table[i];
16+
if (!shmem->place_phys) {
17+
size_t n_pg = NUM_PAGES(shmem->size);
18+
struct ppages ppages = mem_alloc_ppages(shmem->colors, n_pg, false);
19+
if (ppages.num_pages < n_pg) {
20+
ERROR("failed to allocate shared memory");
21+
}
22+
shmem->phys = ppages.base;
23+
}
24+
}
25+
}
26+
27+
struct shmem* shmem_get(size_t shmem_id)
28+
{
29+
if (shmem_id < shmem_table_size) {
30+
return &shmem_table[shmem_id];
31+
} else {
32+
return NULL;
33+
}
34+
}
35+
36+
void shmem_init()
37+
{
38+
if (cpu_is_master()) {
39+
shmem_table_size = config.shmemlist_size;
40+
shmem_table = config.shmemlist;
41+
shmem_alloc();
42+
43+
for (size_t i = 0; i < config.shmemlist_size; i++) {
44+
config.shmemlist[i].cpu_masters = 0;
45+
}
46+
}
47+
}

src/core/vm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <mem.h>
99
#include <cache.h>
1010
#include <config.h>
11+
#include <shmem.h>
1112

1213
static void vm_master_init(struct vm* vm, const struct vm_config* vm_config, vmid_t vm_id)
1314
{
@@ -166,7 +167,7 @@ static void vm_init_ipc(struct vm* vm, const struct vm_config* vm_config)
166167
vm->ipcs = vm_config->platform.ipcs;
167168
for (size_t i = 0; i < vm_config->platform.ipc_num; i++) {
168169
struct ipc* ipc = &vm_config->platform.ipcs[i];
169-
struct shmem* shmem = ipc_get_shmem(ipc->shmem_id);
170+
struct shmem* shmem = shmem_get(ipc->shmem_id);
170171
if (shmem == NULL) {
171172
WARNING("Invalid shmem id in configuration. Ignored.");
172173
continue;

src/core/vmm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <spinlock.h>
1111
#include <fences.h>
1212
#include <string.h>
13-
#include <ipc.h>
13+
#include <shmem.h>
1414

1515
static struct vm_assignment {
1616
spinlock_t lock;
@@ -127,7 +127,7 @@ void vmm_init()
127127
{
128128
vmm_arch_init();
129129
vmm_io_init();
130-
ipc_init();
130+
shmem_init();
131131

132132
cpu_sync_barrier(&cpu_glb_sync);
133133

0 commit comments

Comments
 (0)