Skip to content

Commit

Permalink
Merge pull request #17 from THU-DSP-LAB/16_local_memory_alloc
Browse files Browse the repository at this point in the history
[fix] fixed the memory access bug while load elf
  • Loading branch information
yangzexia authored Nov 29, 2023
2 parents 1b2967e + 4e2f0e2 commit 146367d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
63 changes: 55 additions & 8 deletions spike_main/spike_device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ static std::vector<int> parse_hartids(const char *s)



#define VBASEADDR 0x70000000
#define VBASEADDR 0x70000000

#define ARGBASEADDR 0x90000000


spike_device::spike_device():sim(NULL),buffer(),buffer_data(){
Expand All @@ -190,23 +192,67 @@ spike_device::spike_device():sim(NULL),buffer(),buffer_data(){
uint64_t lds_vaddr;
uint64_t pc_src_vaddr;
printf("spike device initialize: allocating local memory: ");
alloc_local_mem(0x10000000,&lds_vaddr);
alloc_const_mem(0x10000000,&lds_vaddr);
printf("spike device initialize: allocating pc source memory: ");
alloc_local_mem(0x10000000, &pc_src_vaddr);
alloc_const_mem(0x10000000, &pc_src_vaddr);
};

spike_device::~spike_device(){
delete sim;delete[] srcfilename,logfilename;
for (auto& mem : buffer_data)
if(mem.second!=nullptr) {delete mem.second;mem.second=nullptr;}
const_buffer.clear();
for (auto& mem : const_buffer_data)
if(mem.second!=nullptr) {delete mem.second;mem.second=nullptr;}
const_buffer_data.clear();
}

int spike_device::alloc_const_mem(uint64_t size, uint64_t *vaddr) {
uint64_t base;
#define PGSHIFT 12
const reg_t PGSIZE = 1 << PGSHIFT;
if(size <= 0 || vaddr == nullptr )
return -1;
if(const_buffer.empty()){
base=VBASEADDR;
}
else{
base=const_buffer.back().base+const_buffer.back().size;
}


auto base0 = base, size0 = size;
size += base0 % PGSIZE;
base -= base0 % PGSIZE;
if (size % PGSIZE != 0)
size += PGSIZE - size % PGSIZE;

if (base + size < base)
help();

if (size != size0) {
fprintf(stderr, "Warning: the memory at [0x%lX, 0x%lX] has been realigned\n"
"to the %ld KiB page size: [0x%lX, 0x%lX]\n",
base0, base0 + size0 - 1, long(PGSIZE / 1024), base, base + size - 1);
}

printf("to allocate at 0x%lx with %ld bytes \n",base,size);

const_buffer.push_back(mem_cfg_t(reg_t(base),reg_t(size)));
const_buffer_data.push_back(std::pair(base,new mem_t(size)));

*vaddr = base;
return 0;
}

int spike_device::alloc_local_mem(uint64_t size, uint64_t *vaddr){
uint64_t base;
#define PGSHIFT 12
const reg_t PGSIZE = 1 << PGSHIFT;
if(size <= 0 || vaddr == nullptr )
return -1;
if(buffer.empty()){
base=VBASEADDR;
base=ARGBASEADDR;
}
else{
base=buffer.back().base+buffer.back().size;
Expand Down Expand Up @@ -239,7 +285,7 @@ int spike_device::alloc_local_mem(uint64_t size, uint64_t *vaddr){

int spike_device::free_local_mem(){
buffer.clear();
for (auto& mem : buffer_data)
for (auto& mem : buffer_data)
if(mem.second!=nullptr) {delete mem.second;mem.second=nullptr;}
buffer_data.clear();
return 0;
Expand Down Expand Up @@ -551,9 +597,10 @@ int spike_device::run(meta_data* knl_data,uint64_t knl_start_pc){
cfg.hartids = default_hartids;
}

sim=new sim_t(&cfg, halted,
buffer_data, plugin_devices, htif_args, dm_config, log_path, dtb_enabled, dtb_file,cmd_file);

std::vector<std::pair<reg_t, mem_t*>> all_buffer_data(const_buffer_data);
for(auto ele : buffer_data) all_buffer_data.push_back(ele);
sim=new sim_t(&cfg, halted,
all_buffer_data, plugin_devices, htif_args, dm_config, log_path, dtb_enabled, dtb_file,cmd_file);
std::unique_ptr<remote_bitbang_t> remote_bitbang((remote_bitbang_t *) NULL);
/*std::unique_ptr<jtag_dtm_t> jtag_dtm(new jtag_dtm_t(sim->debug_module, dmi_rti));
if (use_rbb) {
Expand Down
3 changes: 3 additions & 0 deletions spike_main/spike_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class spike_device{
public:
spike_device();
~spike_device();
int alloc_const_mem(uint64_t size, uint64_t *dev_maddr);
int alloc_local_mem(uint64_t size, uint64_t *dev_maddr);
int free_local_mem();
int copy_to_dev(uint64_t dev_maddr, uint64_t size,const void* data);
Expand All @@ -86,6 +87,8 @@ class spike_device{
sim_t* sim;
std::vector<mem_cfg_t> buffer; //可以在分配时让buffer地址对齐4k
std::vector<std::pair<reg_t, mem_t*>> buffer_data;
std::vector<mem_cfg_t> const_buffer;
std::vector<std::pair<reg_t, mem_t*>> const_buffer_data;
char* srcfilename;
char* logfilename;
};
Expand Down

0 comments on commit 146367d

Please sign in to comment.