ecall(environment call),当我们在 S 态执行这条指令时,会触发一个 ecall-from-s-mode-exception,从而进入 M 模式中的中断处理流程(如设置定时器等);当我们在 U 态执行这条指令时,会触发一个 ecall-from-u-mode-exception,从而进入 S 模式中的中断处理流程(常用来进行系统调用)。
// kern/driver/console.c#include <sbi.h>#include <console.h>voidcons_putc(int c) { sbi_console_putchar((unsignedchar)c); }
stdio.c里面实现了一些函数,注意我们已经实现了ucore版本的puts函数: cputs()
// kern/libs/stdio.c#include<console.h>#include<defs.h>#include<stdio.h>/* HIGH level console I/O *//* * * cputch - writes a single character @c to stdout, and it will * increace the value of counter pointed by @cnt. * */staticvoidcputch(int c,int*cnt) {cons_putc(c); (*cnt)++;}/* cputchar - writes a single character to stdout */voidcputchar(int c) { cons_putc(c); }intcputs(constchar*str) {int cnt =0;char c;while ((c =*str++) !='\0') {cputch(c,&cnt); }cputch('\n',&cnt);return cnt;}
// libs/defs.h#ifndef__LIBS_DEFS_H__#define__LIBS_DEFS_H__.../* Represents true-or-false values */typedefintbool;/* Explicitly-sized versions of integer types */typedefcharint8_t;typedefunsignedcharuint8_t;typedefshortint16_t;typedefunsignedshortuint16_t;typedefintint32_t;typedefunsignedintuint32_t;typedeflonglongint64_t;typedefunsignedlonglonguint64_t;.../* * * Rounding operations (efficient when n is a power of 2) * Round down to the nearest multiple of n * */#defineROUNDDOWN(a, n) ({ \size_t __a = (size_t)(a); \ (typeof(a))(__a - __a % (n)); \ })...#endif