Skip to content

Conversation

@74rina
Copy link
Member

@74rina 74rina commented Dec 13, 2025

commonのvprintfで、特権に依存するputcharを直接呼ばないように変更しました(引数のputcで、kputchar(カーネル)またはputchar(ユーザー)を指定する形です)。

ユーザーのprintf → vprintf( putchar{ syscall } )
カーネルのkprintf → vprintf( kputchar{ sbi_call } )

という構成にしました。

正しい実装か分からず、レビューお願いいたします😭

Copy link
Contributor

@no2ca no2ca left a comment

Choose a reason for hiding this comment

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

putchar/kputcharがどこで設定されているか分かりやすくて良いと思いました👍

@Latte72R
Copy link
Contributor

@74rina
vprintf はstdcの関数なので中間処理をする関数を作ったほうがstdc互換を保てていいかも

int vprintf(const char *fmt, va_list vargs) {
    return _vprintf(putchar, fmt, vargs);
}

int _vprintf(putc_fn_t putc, const char *fmt, va_list vargs) {
    ...
}

@lemolatoon
Copy link
Member

common.c

void __common_putc(char c);

int vprintf(putc_fn_t putc, const char *fmt, va_list vargs) {
  ...
  __common_putc(...);
  return count;
}

kernel.c

void kputchar(char ch) {...}
void __common_putc(char ch) __attribute__((alias("kputchar")));

usys.c

void putchar(char ch) {...}
void __common_putc(char ch) __attribute__((alias("putchar")));

とする手もあり、どっちがきれいかは諸説あり

Copy link
Contributor

@Latte72R Latte72R left a comment

Choose a reason for hiding this comment

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

kernel/kernel.c Outdated
void kputchar(char ch) { sbi_call(ch, 0, 0, 0, 0, 0, 0, 1); }
void __common_putc(char ch) __attribute__((alias("kputchar")));

void putchar(char ch) { kputchar(ch); }
Copy link
Contributor

Choose a reason for hiding this comment

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

ここの putchar は必要ないです.

common/common.c Outdated
#include "common.h"

int vprintf(const char *fmt, va_list vargs) {
void __common_putc(char c);
Copy link
Contributor

@Latte72R Latte72R Dec 28, 2025

Choose a reason for hiding this comment

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

関数の宣言はデフォルトで extern なので必要はないけど,extern void __common_putc(char c); のほうが分かりやすいかも.

common/common.c Outdated
int vprintf(const char *fmt, va_list vargs) {
void __common_putc(char c);

int vprintf(putc_fn_t __common_putc, const char *fmt, va_list vargs) {
Copy link
Contributor

Choose a reason for hiding this comment

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

putc_fn_t __common_putc を受け取る必要はないです.

common/common.h Outdated

int printf(const char *fmt, ...);
int vprintf(const char *fmt, va_list ap);
int vprintf(putc_fn_t putc, const char *fmt, va_list vargs);
Copy link
Contributor

Choose a reason for hiding this comment

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

putc_fn_t __common_putc は必要ないです.(上に同じく)

typedef uint32_t paddr_t;
typedef uint32_t vaddr_t;
typedef uint8_t bool;
typedef void (*putc_fn_t)(char);
Copy link
Contributor

Choose a reason for hiding this comment

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

putc_fn_t __common_putc を廃止するので,これも必要ないです.

kernel/kernel.c Outdated
va_list vargs;
va_start(vargs, fmt);
int ret = kvprintf(fmt, vargs);
int ret = vprintf(kputchar, fmt, vargs);
Copy link
Contributor

Choose a reason for hiding this comment

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

ここも変更してください.

user/usys.c Outdated
void sys_concat_first_file(void) { syscall(SYS_CAT_FIRST_FILE, 0, 0, 0); }


int vprintf(void (*putc)(char), const char *fmt, va_list vargs);
Copy link
Contributor

Choose a reason for hiding this comment

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

common.h で宣言しているため不要です.

return count;
}

int printf(const char *fmt, ...) {
Copy link
Contributor

Choose a reason for hiding this comment

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

この printf を消す必要はありません.

user/usys.c Outdated


int vprintf(void (*putc)(char), const char *fmt, va_list vargs);
int printf(const char *fmt, ...) {
Copy link
Contributor

Choose a reason for hiding this comment

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

printfcommon.h に置いてください.

int create_file(const char *name, const uint8_t *data, uint32_t size);
void sys_list_root_dir(void);
void sys_concat_first_file(void);
int printf(const char *fmt, ...);
Copy link
Contributor

Choose a reason for hiding this comment

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

printfcommon.h に置いてください.

@Latte72R Latte72R self-requested a review January 11, 2026 02:36
@Latte72R Latte72R merged commit 521af6c into main Jan 11, 2026
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.

6 participants