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

Userspace support: Refactor init to launch a simple device model #547

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

vijaydhanraj
Copy link
Contributor

This PR adds an initial version of init to launch a dummy device model. The initial dm version just calls exit syscall without doing anything else.

Note: This PR is built on top of #520 and #539 PRs.

@joergroedel joergroedel added the wait-for-review PR needs for approval by reviewers label Dec 5, 2024
@vijaydhanraj vijaydhanraj force-pushed the init_dm_module branch 2 times, most recently from 5a5c846 to 0000873 Compare December 10, 2024 05:29
Copy link
Member

@joergroedel joergroedel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a general note the init process should have a configuration file which lists the modules to launch. We should also start thinking about a feature detection mechanism for the SVSM so that init can make decisions based on the environment it is running in. But that is outside the scope of this PR.

Cargo.toml Outdated Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
user/dm/Cargo.toml Outdated Show resolved Hide resolved
user/dm/src/main.rs Outdated Show resolved Hide resolved
Comment on lines +34 to +36
let bin = CString::new("/").unwrap();
let Ok(obj) = opendir(&bin) else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be just opendir(b"/\0")?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opendir expects a &CStr type but using a raw byte slice may cause a compilation error. Also, current implementation is extensible if in future we want to change path from /.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I understand, but creating a CStr usually requires allocator support. I am wondering whether there is a simpler way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about something like this?

-    let bin = CString::new("/").unwrap();
-    let Ok(obj) = opendir(&bin) else {
+    let bin = CStr::from_bytes_with_nul(b"/\0").unwrap();
+    let Ok(obj) = opendir(bin) else {
         return 0;
     };

Is the preference not to use any heap allocator for now? I added buddy_system_allocator only as a temporary solution until we have our own heap allocator.

BTW, I do have an alternate implementation using the stack and avoiding heap allocation entirely. With this implementation we can skip adding buddy_system_allocator crate.

@joergroedel joergroedel added in-review PR is under active review and not yet approved and removed wait-for-review PR needs for approval by reviewers labels Dec 11, 2024
@vijaydhanraj
Copy link
Contributor Author

As a general note the init process should have a configuration file which lists the modules to launch. We should also start thinking about a feature detection mechanism for the SVSM so that init can make decisions based on the environment it is running in. But that is outside the scope of this PR.

Yes, this makes sense. I can look into this.

Add initial version of init to launch a dummy device model.
Use `opendir` and `readdir` syscalls to discover the first
file under "/" and `exec` the dm process.

Signed-off-by: Vijay Dhanraj <[email protected]>
Signed-off-by: Peter Fang <[email protected]>
Binaries without uninitialized global variable may generate a `.bss`
LOAD segment with no valid content (`p_filesz` = 0, `p_memsz` = 0). The
problem with such a segment is that ELF parser may fail with unaligned
segment address error. To avoid this, add a dummy `.bss` to force non-zero
memory allocation, ensuring a meaningful PT_LOAD.

Signed-off-by: Vijay Dhanraj <[email protected]>
The device model is a user mode program which manages VM's life cycles
and handles various vmexits events. The initial version just calls exit()
syscall without doing anything else.

Signed-off-by: Vijay Dhanraj <[email protected]>
Signed-off-by: Chuanxiao Dong <[email protected]>
@vijaydhanraj
Copy link
Contributor Author

Hi @joergroedel can you please take a look?

Comment on lines +27 to 33
// SAFETY: The `begin` and `size` values refer to a valid, properly aligned, and
// accessible memory region.
unsafe {
HEAP_ALLOCATOR
.lock()
.init(core::ptr::addr_of!(HEAP) as usize, HEAP_SIZE);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heap allocator initialization needs to happen in the userlib crate. When main starts the heap should be initialized already.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. However, if we aim to avoid using the heap allocator, this comment may not be applicable. Could you please clarify your preference?

Comment on lines +58 to +63
/* Fallback dummy section if `.bss` is empty */
. = ALIGN(4096);
.bss_dummy (NOLOAD) : {
LONG(0);
} :bss

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate on why this is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this workaround to resolve this issue, #539 (comment). Is there any better way to address this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in-review PR is under active review and not yet approved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants