Giter VIP home page Giter VIP logo

loongson-dune's Introduction

loongson-dune's People

Contributors

limaomao821 avatar martins3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

loongson-dune's Issues

getpid failed when glibc dynamic linked

  • printf 还是正常工作

  • getpid 有时候还是正常返回的

  • syscall 没有被调用

  • 多次调用的结果不相同

  • 如果在进入到 dune 之前进行函数调用,那么就是正确运行

    • dune 没有实现要求的地址保持一致: 如果调用过一次,那个跳转就应该被修改掉了
  • 为什么 gettimeofday 不是在 vdso 中间的 ?

  • 测试 tlbrefill 的方法,分配 1 GB 的空间,然后全部访问一次

debug : syscall emulation

I don't know why the guest can't continue in the exception context, it has to be redirected to the next instruction of syscall.

ltp : expand stack in advance to avoid spurious gup failure

calling a function which demands a huge stack will cause gup fail

void get_writesize(){
  printf("get_writesize\n");
	char read[4096];
  printf("read : %p\n", read);

  char buf[0x3d000];
  printf("buf location %p", buf);
}
➜  test git:(main) ✗ sudo ./stack
  Info: open /dev/kvm
  Info: KVM_GET_API_VERSION
  Info: KVM_CREATE_VM
  Info: KVM_SET_USER_MEMORY_REGION
  Info: ebase address : fff4310000
--- Process Map Dump ---
heee
after open
after fread
after setvbuf
0x000000012001c000-0xffffffffffffffff ---S ffffffffffffffff
0x0000000120028000-0x000000012002c000 RWXP 00018000 /home/loongson/dune/test/stack
0x00000001207c4000-0x00000001207c8000 ---P 00000000 [heap]
0x00000001207c8000-0x00000001207cc000 RWXP 00000000 [heap]
0x000000fff430c000-0x000000fff4314000 RWXP 00000000
0x000000fff4314000-0x000000fff431c000 RWXS 00000000 anon_inode:kvm-vcpu:0
0x000000fff431c000-0x000000fff4320000 RWXP 00000000
0x000000fffbac4000-0x000000fffbae8000 RWXP 00000000 [stack]
0x000000fffbff4000-0x000000fffbff8000 R-XP 00000000
0x000000fffe178000-0x000000fffe180000 R--P 00000000 [vvar]
0x000000fffe180000-0x000000fffe184000 R-XP 00000000 [vdso]
[256480.819242] kvm [3289]: huxueshi trace enter kvm_vz_vcpu_run:3584 cause=40008020
[256480.819250] kvm [3289]: kvm_mips_handle_exit: cause: 0x1080000c, PC: 120007804, kvm_run: 000000001af98063, kvm_vcpu: 0000000042c2abdb cause=40008020
[256480.819253] kvm [3289]: TLB ST fault:  cause 0x1080000c, status 0x740000a0, PC: 00000000f229a1c2, BadVaddr: 0xfffbaa5650
[256480.819257] kvm [3289]: Failed to find VMA for hva 0xfffbaa4000
[256480.819260] kvm [3289]: update_pc(): New PC: 0x120007808
[256480.819263] kvm [3289]: [0x120007808] OP_SD: eaddr: 0xfffbaa5650, gpr: 0x120032610, data: 0x120032610
[256480.819267] kvm [3289]: kvm_mips_handle_exit : cause register = 40008020 exit_reason=6
[256480.819269] kvm [3289]: huxueshi trace leave kvm_vz_vcpu_run:3586 cause=40008020

Yes, it breaks out of the stack boundary, the stack size is 0x24000

kill10 failed

因为 child 在创建出来虚拟机之前就会收到信号,在模拟的 fork 返回的位置,信号处理函数正确执行,之后模拟并没有这个信号的干扰。

kernel mode : tlb

  • kvm_vz_vcpu_load_tlb
  • kvm_vz_vcpu_load_wired
    • why load tlb in kvm_vz_vcpu_run instead of everytime vmexit

TLB need special attention

  1. should we copy TLB used in host to guest mode ?

Copy code from tlb.c is error-prone

  • sharing global variable with mips kvm maybe disturb kvm

We shouldn't remove request related code

  • vz:requests and remote tlb flush

We shouldn't remove mode related code

  • vz_vcpu::mode , check it's corresponding code in the kvm and find all the code

fork crashed again

  • fork 之后,parent 和 child 之间的 pid 距离为 3,正常应该是 1 的

kernel mode : mmu

Something related to emualted:

  • mapped and unmapped
    • kvm_mips_gpa_pte_to_gva_unmapped, kvm_mips_gpa_pte_to_gva_mapped
    • kvm_mips_handle_kseg0_tlb_fault <= kvm_trap_emul_gva_fault <= kvm_get_inst <= used by emulate.c
  • kseg0
    • kvm_mips_handle_kseg0_tlb_fault
  • emulated
    • x: kvm_trap_emul_gva_fault
      • x: kvm_get_inst
    • kvm_mips_handle_mapped_seg_tlb_fault : caller is emulate

Boring stuff

  • move mmu notifier to mmu.c
  • kvm_pgd_alloc
  • examine transparent hugepage
  • dune_arch_vcpu_put : some effort to complete the code
  • kvm_mips_handle_commpage_tlb_fault : now that commpage is used for tlb, should dune remove it ?
  • remove kvm_host.c
  • remove dirty bit tracking in mmu.c

vcpu, ept and guest page table

  • A sthread fork will create two guest page table
  • A Linux fork with VM clone will create two
  • A dune_enter doesn't create user space percpu
    • dune_enter create a new vcpu and ept

kvm_memory_slot

  • maybe we should take advantage of it
  • move memory_slot related code into one place

Road map

review mode

  • how kvm_main works

ideas

  • host should work in another thread, then anything did in host_loop just like another thread, so it works just like two threads run in parallel.

user mode : page walk

  • page flags
  • PDX
  • level
  • pgroot
    • cr3 for mips ?
  • vm.c : dune_vm_default_pgflt_handler : we will not handle it until I finished hello world

Dynamic / Static Binary Modification

  • find the book : Dynamic Binary Modification
  • how x86 dune take advantage of minimal loader, why not create a similar project for this?
  • proxychain can redict all kinds of network related syscall, can I use similar way?

escape dune

  • a special syscall to escape to host
  • a setjmp and long jump to just to where issue the syscall.

kernel mode : init

  • init
    • seems no hardware enable is needed, kind of unbelieveable
    • hardware_enable_all
    • kvm_vz_hardware_enable
    • kvm_vz_vcpu_put
  • entry setup
    • remove kvm_host.h : kvm_vcpu and arch_vcpu
    • register the entry
  • hypercall / skip one instruction
  • PF_VCPU
	if (current->flags & PF_VCPU)
		kvm_vz_vcpu_save_wired(vcpu);

what should be inited and how?

currently, I just want to do a following how dune works:

  • dune_enter => vz_launch => create_vcpu && create_vm (Yes, VM and VCPU is one-to-one pair)
  • dune_init(this is module related init)

use Loongson VZ instead of VZ

  • we will diff the code later

kvm_mips_handle_exit : EXCCODE_INT

[182208.357867] kvm [22087]: kvm_mips_handle_exit : cause register = 40008000 exit_reason=0
[182208.357926] kvm [22087]: kvm_mips_handle_exit: cause: 0x10804000, PC: 1200018c8, kvm_run: 00000000a3f95d25, kvm_vcpu: 000000001c3b428a cause=40008000
[182208.357931] kvm [22087]: [0]EXCCODE_INT @ 1200018c8
[182208.357934] kvm [22087]: kvm_mips_handle_exit : cause register = 40008000 exit_reason=0
[182208.361137] kvm [22087]: kvm_mips_handle_exit: cause: 0xd0808000, PC: 1200018d4, kvm_run: 00000000a3f95d25, kvm_vcpu: 000000001c3b428a cause=40008000
[182208.361139] kvm [22087]: [0]EXCCODE_INT @ 1200018d4
[182208.361142] kvm [22087]: kvm_mips_handle_exit : cause register = 40008000 exit_reason=0
[182208.365043] kvm [22087]: kvm_mips_handle_exit: cause: 0x50808000, PC: 1200018c8, kvm_run: 00000000a3f95d25, kvm_vcpu: 000000001c3b428a cause=40008000
[182208.365045] kvm [22087]: [0]EXCCODE_INT @ 1200018c8
[182208.365048] kvm [22087]: kvm_mips_handle_exit : cause register = 40008000 exit_reason=0
[182208.368950] kvm [22087]: kvm_mips_handle_exit: cause: 0x50808000, PC: 1200018c8, kvm_run: 00000000a3f95d25, kvm_vcpu: 000000001c3b428a cause=40008000
[182208.368952] kvm [22087]: [0]EXCCODE_INT @ 1200018c8
[182208.368957] kvm [22087]: kvm_mips_handle_exit : cause register = 40008000 exit_reason=0
[182208.371330] kvm [22087]: kvm_mips_handle_exit: cause: 0x10804000, PC: 1200018c8, kvm_run: 00000000a3f95d25, kvm_vcpu: 000000001c3b428a cause=40008000
[182208.371332] kvm [22087]: [0]EXCCODE_INT @ 1200018c8

ltp: fork09 failed

cmdline="dune-fork09 "
contacts=""
analysis=exit
<<<test_output>>>
  Info: open /dev/kvm
  Info: KVM_GET_API_VERSION
  Info: KVM_CREATE_VM
  Info: KVM_SET_USER_MEMORY_REGION
  Info: ebase address : fff6ea0000
fork09      0  TINFO  :  OPEN_MAX is 1024
fork09      0  TINFO  :  first file descriptor is 6 
fork09      0  TINFO  :  Parent reporting 1023 files open
�[36m Error: unable to open /dev/kvm
�[0m�[36m Error: setup_vm_with_one_cpu
�[0m�[31m  Fatal: dup_vm
�[0m  Info: open /dev/kvm
  Info: KVM_GET_API_VERSION
  Info: KVM_CREATE_VM
  Info: KVM_SET_USER_MEMORY_REGION
  Info: ebase address : fff6ea0000

pipe

pipe is special

in /home/maritns3/core/loongson-dune/cross/arch/mips/kernel/scall64-64.S

EXPORT(sys_call_table)
	PTR	sys_read			/* 5000 */
	PTR	sys_write
	PTR	sys_open
	PTR	sys_close
	PTR	sys_newstat
	PTR	sys_newfstat			/* 5005 */
	PTR	sys_newlstat
	PTR	sys_poll
	PTR	sys_lseek
	PTR	sys_mips_mmap
	PTR	sys_mprotect			/* 5010 */
	PTR	sys_munmap
	PTR	sys_brk
	PTR	sys_rt_sigaction
	PTR	sys_rt_sigprocmask
	PTR	sys_ioctl			/* 5015 */
	PTR	sys_pread64
	PTR	sys_pwrite64
	PTR	sys_readv
	PTR	sys_writev
	PTR	sys_access			/* 5020 */
	PTR	sysm_pipe
	PTR	sys_select

Everybody started with sys_ but pipe started with sysm_

/home/maritns3/core/musl/src/unistd/mips64/pipe.s

.set	noreorder
.global	pipe
.type	pipe,@function
pipe:
	lui	$3, %hi(%neg(%gp_rel(pipe)))
	daddiu	$3, $3, %lo(%neg(%gp_rel(pipe)))
	daddu	$3, $3, $25
	li	$2, 5021
	syscall
	beq	$7, $0, 1f
	nop
	ld	$25, %got_disp(__syscall_ret)($3)
	jr	$25
	dsubu	$4, $0, $2
1:	sw	$2, 0($4)
	sw	$3, 4($4)
	move	$2, $0
	jr	$ra
	nop

ltp: fcntl34

Sometimes, it will crash.

  • understand how stack works. That's right, we can't understand what we are writing.

MIPS assembly

#include <stdio.h>

int
main(void)
{
  asm("jmp label");
  puts("You should not see this.");
  asm("label:");

  return 0;
}
 unsigned int x;
 asm volatile ("move %0, $ra" : "=r" (x));
 asm volatile ("sw $ra, %0" : "=m" (x));

why virtual address is 40 bit long?

void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
{
	unsigned long random_factor = 0UL;

	if (current->flags & PF_RANDOMIZE)
		random_factor = arch_mmap_rnd();

	if (mmap_is_legacy(rlim_stack)) {
		mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
		mm->get_unmapped_area = arch_get_unmapped_area;
	} else {
		mm->mmap_base = mmap_base(random_factor, rlim_stack);
		mm->get_unmapped_area = arch_get_unmapped_area_topdown;
	}
}

Legacy or not makes a difference?

ltp: we can support exec like syscall

Although dune can't make the program which is a parameter of syscall exec run in a virtualization environment, making the program run correctly without too many limitations is always better.

prtcl04 failed

tst_test.c:1313: TINFO: Timeout per run is 0h 05m 00s
  Info: KVM_CREATE_VM
  Info: KVM_CREATE_VCPU
prctl04.c:213: TINFO: kernel support PR_GET/SET_SECCOMP
  Info: KVM_CREATE_VM
  Info: KVM_CREATE_VCPU
prctl04.c:197: TPASS: SECCOMP_MODE_STRICT doesn't permit GET_SECCOMP call
  Info: KVM_CREATE_VM
  Info: KVM_CREATE_VCPU
prctl04.c:199: TFAIL: SECCOMP_MODE_STRICT doesn't permit read(2) write(2) and _exit(2)
  Info: KVM_CREATE_VM
  Info: KVM_CREATE_VCPU
prctl04.c:197: TPASS: SECCOMP_MODE_STRICT doesn't permit close(2)
  Info: KVM_CREATE_VM
  Info: KVM_CREATE_VCPU
prctl04.c:197: TPASS: SECCOMP_MODE_FILTER doestn't permit GET_SECCOMP call
  Info: KVM_CREATE_VM
  Info: KVM_CREATE_VCPU
prctl04.c:199: TFAIL: SECCOMP_MODE_FILTER doesn't permit close(2)
  Info: KVM_CREATE_VM
  Info: KVM_CREATE_VCPU
prctl04.c:197: TPASS: SECCOMP_MODE_FILTER doesn't permit exit()
  Info: KVM_CREATE_VM
  Info: KVM_CREATE_VCPU
prctl04.c:199: TFAIL: SECCOMP_MODE_FILTER doesn't permit exit()

ltp: test case

  • 还有,按道理来说,即使是将 dune_enter 放到 parent process 上,也不应该出现 pr_err("KVM_CREATE_VM"); 的错误。

How to transport to Loongarch?

Maybe we should set up a standard interface for developers to port to another system.

  • musl / gcc
    • how clone works
  • ltp setup
  • kernel
    • .config
    • regs get and set method
  • manual
    • SIMD and float save and restore
    • general register save and restore
    • TLB refill entry
    • TLB refill
    • CSR register setup
    • Physical Address Range
    • syscall entry
    • xphysicall offset
    • vmcall
  • ABI
    • callee saved and caller saved register
    • gcc asm
      • .set noat
      • .set noreorder
    • syscall
    • why $2 is used for TLS register

why synchronize timerid failed?

从 vcpu_load 的位置切换到 kvm_vz_vcpu_run 大大的减少了发生错误的情况,但是还是存在一些这种情况。

  • 至少说明,现在这些情况的确是由于 timerid 的刷新导致的
  • 既然 vcpu_load 的实现总是正确的,那么这种同步为什么出错?

ltp: mmap16

➜  mmap git:(master) sudo  ./dune-mmap16
  Info: top = fffb800000, limit = 800000
  Info: open /dev/kvm
  Info: KVM_GET_API_VERSION
  Info: KVM_CREATE_VM
  Info: KVM_SET_USER_MEMORY_REGION
  Info: ebase address : fff6620000
mmap16      0  TINFO  :  Found free device 0 '/dev/loop0'
mmap16      0  TINFO  :  Formatting /dev/loop0 with ext4 opts='-b 1024' extra opts='10240'
mmap16      1  TBROK  :  mmap16.c:168: mkfs.ext4 failed with exit code 128
mmap16      2  TBROK  :  mmap16.c:168: Remaining cases broken

I expected it failed with "unsupported syscall", but didn't

ltp: Filter

No More vcpu

  • fcntl36
  • init_module01

Need more

  • dune-fork09

  • inotify09 is much more slowly than works

debug : hypercall

  • I think HYPCALL instruction works, just GCC doesn't support it
    • kernel module containing the 0x42000028 will crash the kernel, but random instruction cause readable error
  • how to cause the gup?
    • make KVM recompile easy and debug in the kernel
    • DEBUG option
    • so, check the vmexit ?

trifle

  • change boot time from 5s to 0s

user mode : entry

Context Switch && Interrupt, here I came.

  • dune_config
  • dune_trap_regs : general purpose regs + rip + rflags, we can ignore it savely.

ltp: vm_id

It's used for debugging the fork syscall, there is no need to worry about it, but cpu_id is more critical.

ltp : KVM_EXIT_INTR

  • create a program that we will come across KVM_EXIT_INTR 100%
  • If I define a signal handler, will it handled in guest space or host space? (host)

ltp: float and simd

image
--------------------------page break-----------------------
image

MIPS Virtualization Manual

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.