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

Add some libc function #34

Closed
wants to merge 13 commits into from
Closed

Add some libc function #34

wants to merge 13 commits into from

Conversation

coolyjg
Copy link
Contributor

@coolyjg coolyjg commented Apr 28, 2023

Finish some libc implementations.

stdlib.h

  • calloc: ax_malloc + memset
  • realloc declaration: Previous code lacks of declaration
  • strtol, atoll, strtoll, strtoul, strtoull: Some type conversion functions
  • exit: call ax_exit
  • abs, llabs: Get absolution value

stdio.h

  • Change stdin, stdout, stderr definition to FILE*. Accordingly change corresponding print-like functions using stdout->fd rather than number 1.
  • fopen: Since libc are responsible for allocating FILE* structure in fopen, this function acquires both "fs" and "alloc" feature.
  • fflush: Change input variable type.
  • vsnprintf, snprintf, vsprintf, vfprintf, sprintf: Some print-like implementation inspired by previous fprintf implementation.

time.h

  • clock_gettime, gmtime, time, local_time, gettimeofday: Some time implementation based on ulib/libax/src/time.rs
  • nanosleep: Call sleep API in task. TODO: task can be early woken up by some signal and return remained time.

unistd.h

  • usleep, sleep: Call sleep API in task
  • getpid: Call current() if "multitask" feature is on, or return 2 ("main" task Id)

Add some cbindings syscall implementation

  • ax_exit
  • ax_clock_gettime
  • ax_nanosleep

@coolyjg coolyjg force-pushed the libc-dev branch 2 times, most recently from a678417 to a857300 Compare April 28, 2023 07:52
ulib/libax/src/time.rs Outdated Show resolved Hide resolved
ulib/libax/src/cbindings/time.rs Outdated Show resolved Hide resolved
ulib/libax/src/cbindings/time.rs Show resolved Hide resolved
ulib/libax/src/cbindings/time.rs Outdated Show resolved Hide resolved
ulib/libax/src/cbindings/ctask.rs Outdated Show resolved Hide resolved
ulib/c_libax/include/limits.h Outdated Show resolved Hide resolved
ulib/c_libax/include/stdio.h Outdated Show resolved Hide resolved
ulib/c_libax/src/stdio.c Outdated Show resolved Hide resolved
ulib/c_libax/src/stdio.c Outdated Show resolved Hide resolved
ulib/libax/src/cbindings/fs.rs Outdated Show resolved Hide resolved
@coolyjg
Copy link
Contributor Author

coolyjg commented Apr 28, 2023

  • Fixed problems mentioned above
    • remove non-std-like Instant function
    • check time related variables validation
    • merge ctask.rs and thread.rstothread.rs`
    • remove ax_time, only reserve ax_clock_gettime
    • call axhal::time::current_time() in ax_clock_gettime
    • remove ax_sleep, ax_usleep, reserver ax_nanosleep
    • merge stdin.c, stdout.c, stderr.c into stdio.c
    • remove ax_fopen, and re-implemente fopen with ax_open
  • Add strtoll, strtoul, strtoull implementation inspired by unikraft.
  • getchar() remains unsolved.

Copy link
Member

@equation314 equation314 Apr 30, 2023

Choose a reason for hiding this comment

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

Use #define for simple functions?

#define isalpha(a) (((unsigned)(a)|32)-'a') < 26)
// ...

f = (FILE *)malloc(sizeof(FILE));

flags = __fmodeflags(mode);
int fd = ax_open(filename, flags, mode);
Copy link
Member

Choose a reason for hiding this comment

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

mode type in ax_open is not char *

#define CLOCK_MONOTONIC 1
#define CLOCKS_PER_SEC 1000000L

#define ax_time_usec_to_nsec(usec) ((usec)*1000UL)
Copy link
Member

Choose a reason for hiding this comment

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

This definition is useless, use (usec)*1000 in code directly.

pub unsafe extern "C" fn ax_getpid() -> c_int {
ax_call_body!(ax_getpid, {
let pid = current().id().as_u64() as c_int;
debug!("getpid return {}", pid);
Copy link
Member

Choose a reason for hiding this comment

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

This debug info is already printed in ax_call_body


use crate::debug;
use crate::task::exit;
use axerrno::{LinuxError, LinuxResult};
Copy link
Member

@equation314 equation314 Apr 30, 2023

Choose a reason for hiding this comment

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

Remove unused imports

unsafe {
*ts = ret;
}
debug!("ax_clock_gettime: {}s, {}ns", now.as_secs(), now.as_nanos());
Copy link
Member

Choose a reason for hiding this comment

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

debug!("ax_clock_gettime: {}.{:09}s", ret.tv_sec, ret.tv_nsec);

return Err(LinuxError::EINVAL);
}

let total_nano = (*req).tv_sec as u64 * NANOS_PER_SEC + (*req).tv_nsec as u64;
Copy link
Member

@equation314 equation314 Apr 30, 2023

Choose a reason for hiding this comment

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

Add debug info for arguments:

debug!("ax_nanosleep <= {}.{:09}s", (*req).tv_sec, (*req).tv_nsec);

@chyyuu
Copy link
Member

chyyuu commented May 4, 2023

@coolyjg Very good! try to make your work better.

@coolyjg
Copy link
Contributor Author

coolyjg commented May 4, 2023

This PR will be clearer in #38

@coolyjg coolyjg closed this May 4, 2023
@coolyjg coolyjg deleted the libc-dev branch May 7, 2023 05:19
hbuxiaofei pushed a commit to hbuxiaofei/arceos that referenced this pull request Apr 29, 2024
hky1999 added a commit that referenced this pull request Jan 1, 2025
* [feat] update allocator version to support alloc_pages_at
* [feat] set phys-virt-offset as 0xffff_8000_0000_0000 for x86_64 qemu
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

Successfully merging this pull request may close these issues.

5 participants