Saturday, 18 January 2020

[xv6] adventure

Build & Startup

Every change has to be (re)built by
  • make
Run the xv6 OS (without pop-up) by

  • make qemu-nox

Or in debug mode by (cover elsewhere)

  • make qemu-nox-gdb

Add Command

says, there is sample.c to be a command in xv6

  • system call required?
    • vi defs.h     (add forward declaration)

    • // proc.c
      ......
      int             csc ( void ); 
    • vi proc.c     (add function to this)
//custom process named "csc()"intcsc(){......}
    • vi syscall.h  (define sys call vector)
// System call numbers #define SYS_fork    1 ......#define SYS_close  21 #define SYS_csc    22
    • vi syscall.c  (connect shell and kernel by extern)

extern int sys_chdir(void);
......
extern int sys_csc(void);
......
static int (*syscalls[])(void) = {
......
[SYS_csc]     sys_csc,
};    
    • vi user.h     (callable function via shell)

// system calls
......
int csc ( void );
    • vi sysproc.c  (implement the method)

......
int
sys_csc ( void )
{
  return csc ();
    • vi usys.S     (marco to connect user call to sys call)
......SYSCALL(csc)
    • vi sysfunc.h  (add sys call handler)
  • put sample.c into xv6 folder
#include "types.h"......intmain(int argc, char *argv[]){  csc();  exit();}
  • vi Makefile

UPROGS=\
        _cat\
        ......
        _sample\
        ......
......
EXTRA=\
        ......
        ln.c ...... sample.c ......\
        ......
  • make              (Build the whole OS)

  1. aas

Add Executable

says, step by step
  • prepare C++ codes and header files
    • vi example.cpp     (function)
using namespace std;
double example(double a, double b){
  return a + b;
}
    • vi plus.h
#ifndef A_Hdouble example(double a, double b);#endif
    • vi main.cpp    (main program)
#include <iostream>#include <stdlib.h>#include "plus.h"using namespace std;int main(int argc, char *argv[0]){  double x, y;  x = atof(argv[1]);  y = atof(argv[2]);  cout << plus(x, y) << endl;  return 0;}
  • compile the codes to objects (plus.o & main.o)
    • g++ -c example.cpp
    • g++ -c main.cpp
  • link objects to executable (plus)
    • g++ -o plus example.o main.o
  • execute it
    • ./plus
Or, in one go

  • create a Makefile
    • vi Makefile
# default targetplus: example.o main.o        # target: prerequistites/dependencies        g++ -o plus example.o main.oexample.o main.o:             # prerequistites/dependencies:        g++ -c example.cpp        g++ -c main.cppclean:                        # other target        rm example.o main.o plus
Or with marco constantPROG   = plusSRCS   = example.cpp main.cppOBJS   = $(SRCS:.cpp=.o)      # replace .o to .cpp in SRCS# default target$(PROG): $(OBJS)              # target: prerequistites/dependencies        g++ -o $@ $(OBJS)$(OBJS):                      # prerequistites/dependencies:        g++ -c $*.cppclean:                        # other target        rm $(OBJS) $(PROG)
  • simply use either
    • make
    • make plus
  • to remove generated files
    • make clean












g++ -o aShell aShell.cpp

No comments:

Post a Comment

vm - Centos - Boot into latest Kernel

sudo grub2-set-default 0 sudo grub2-mkconfig -o /boot/grub2/grub.cfg sudo reboot