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

Support mmap on shecc #69

Closed
WangHanChi opened this issue Aug 7, 2023 · 2 comments
Closed

Support mmap on shecc #69

WangHanChi opened this issue Aug 7, 2023 · 2 comments

Comments

@WangHanChi
Copy link
Contributor

I am trying to implement mmap2 for shecc, but I found that there are no enough registers for calling the syscall based on test_mmap.s. So, I modified OP_syscall in src/riscv-codegen.c as following,

case OP_syscall:
            emit(__addi(__a7, __a0, 0));
            emit(__addi(__a0, __a1, 0));
            emit(__addi(__a1, __a2, 0));
            emit(__addi(__a2, __a3, 0));
+           emit(__addi(__a3, __a4, 0));
+           emit(__addi(__a4, __a5, 0));
+           emit(__addi(__a5, __a6, 0));
            emit(__ecall());
            if (dump_ir == 1)
                printf("    syscall");
            break;

On the other hand, to test the syscall, I modified the malloc help function as following:

block_meta_t *__malloc_request_space(int size)
{
    char *brk;
    block_meta_t *block;

+   void *tmp = __syscall(__syscall_mmap2, 0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);

    brk = __syscall(__syscall_brk, 0);

After the mentioned modification, the segmentation fault is occurred when using qemu to execute the code.

$ make VERBOSE=1
out/inliner lib/c.c out/libc.inc
cc -o out/src/main.o -O -g -ansi -pedantic -Wall -Wextra -c -MMD -MF out/src/main.o.d src/main.c
cc out/src/main.o -o out/shecc
out/shecc --dump-ir -o out/shecc-stage1.elf src/main.c > out/shecc-stage1.log
chmod a+x out/shecc-stage1.elf
/usr/bin/qemu-riscv32 out/shecc-stage1.elf -o out/shecc-stage2.elf src/main.c
make: *** [Makefile:76: out/shecc-stage2.elf] Segmentation fault (core dumped)

Then, I dump the disassembly code to check. The caller and callee are shown below:

  • Caller:
   123e4:	ff010113          	addi	sp,sp,-16
   123e8:	00a12023          	sw	a0,0(sp)
   123ec:	0de00513          	li	a0,222
   123f0:	00000593          	li	a1,0
   123f4:	00001637          	lui	a2,0x1
   123f8:	00060613          	mv	a2,a2
   123fc:	00300693          	li	a3,3
   12400:	02200713          	li	a4,34
   12404:	fff00793          	li	a5,-1
   12408:	00000813          	li	a6,0
   1240c:	c61fd0ef          	jal	ra,0x1006c
  • Callee:
   1006c:	ff010113          	addi	sp,sp,-16
   10070:	00812623          	sw	s0,12(sp)
   10074:	00112423          	sw	ra,8(sp)
   10078:	00010413          	mv	s0,sp
   1007c:	00050893          	mv	a7,a0
   10080:	00058513          	mv	a0,a1
   10084:	00060593          	mv	a1,a2
   10088:	00068613          	mv	a2,a3
   1008c:	00070693          	mv	a3,a4
   10090:	00078713          	mv	a4,a5
   10094:	00080793          	mv	a5,a6
   10098:	00000073          	ecall
   1009c:	01040113          	addi	sp,s0,16
   100a0:	ff812083          	lw	ra,-8(sp)
   100a4:	ffc12403          	lw	s0,-4(sp)
   100a8:	00008067          	ret

It looks like a correct implementation and is similar to the test_mmap.s, but it is failed. I have no idea how to solve it.

@vacantron
Copy link
Collaborator

You need to modify the instruction count of that function after appending the new ones for it. Otherwise, the calculated offset of branch / jump instructions will be incorrect.

shecc/src/riscv-codegen.c

Lines 104 to 105 in eed747b

case OP_syscall:
return 20;

@WangHanChi
Copy link
Contributor Author

Thank you for your response. I made an obvious oversight.
Now it cam work properly, and mmap syscall is also available for use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants