OS-4

Chapter 4: Backbone of the OS and C++ runtime

C++ kernel run-time

A kernel can be programmed in C++, it is very similar to making a kernel in C, except that there are a few pitfalls you must take into account (runtime support, constructors, …)

The compiler will assume that all the necessary C++ runtime support is available by default, but as we are not linking in libsupc++ into your C++ kernel, we need to add some basic functions that can be found in the cxx.cc file.

Caution: The operators new and delete cannot be used before virtual memory and pagination have been initialized.

Basic C/C++ functions

The kernel code can’t use functions from the standard libraries so we need to add some basic functions for managing memory and strings:

void    itoa(char *buf, unsigned long int n, int base);

void *  memset(char *dst,char src, int n);
void *  memcpy(char *dst, char *src, int n);

int     strlen(char *s);
int     strcmp(const char *dst, char *src);
int     strcpy(char *dst,const char *src);
void    strcat(void *dest,const void *src);
char *  strncpy(char *destString, const char *sourceString,int maxLength);
int     strncmp( const char* s1, const char* s2, int c );

These functions are defined in string.cc, memory.cc, itoa.cc

C types

During the next step, we are going to use different types in our code, most of the types we are going to use unsigned types (all the bits are used to stored the integer, in signed types one bit is used to signal the sign):

typedef unsigned char   u8;
typedef unsigned short  u16;
typedef unsigned int    u32;
typedef unsigned long long  u64;

typedef signed char     s8;
typedef signed short    s16;
typedef signed int      s32;
typedef signed long long    s64;
Compile our kernel

Compiling a kernel is not the same thing as compiling a linux executable, we can’t use a standard library and should have no dependencies to the system.

Our Makefile will define the process to compile and link our kernel.

For x86 architecture, the followings arguments will be used for gcc/g++/ld:

# Linker
LD=ld
LDFLAG= -melf_i386 -static  -L ./  -T ./arch/$(ARCH)/linker.ld

# C++ compiler
SC=g++
FLAG= $(INCDIR) -g -O2 -w -trigraphs -fno-builtin  -fno-exceptions -fno-stack-protector -O0 -m32  -fno-rtti -nostdlib -nodefaultlibs 

# Assembly compiler
ASM=nasm
ASMFLAG=-f elf -o
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值