Coder Social home page Coder Social logo

minep / lunaix-os Goto Github PK

View Code? Open in Web Editor NEW
429.0 11.0 50.0 126.85 MB

A simple (yet naive), POSIX-compliant (hopefully!) operating system from scratch!

License: MIT License

Shell 1.57% Assembly 2.59% C 86.28% Makefile 0.81% Python 8.60% NASL 0.15%

lunaix-os's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lunaix-os's Issues

章节6.1内存管理bug

  • 代码位置:(virtual memory) e141fd4

  • 文件:pmm.c

  • 问题函数:pmm_mark_chunk_freepmm_mark_chunk_occupied

  • 描述
    在函数中需要对位图做批处理操作,在处理最后一个页表组中使用到了以下计算:
    (((1U << (page_count > 8 ? remainder : 0)) - 1) << (8 - remainder))
    page_count > 8用于判断是否需要处理最后一个页表组,但是似乎是忽略了页偏移的情况

  • 测试(以pmm_mark_chunk_occupied举例)

    int main() {
        // 假设从位置0x5开始设置6个页已占用
        pmm_mark_chunk_occupied(5, 6);
        for (int i = 0; i < 3; ++i) {
            for (int j = 7; j >= 0; --j) {
                cout << (pm_bitmap[i] >> j & 1);
            }
            cout << endl;
        }
        return 0;
    }
  • 运行结果

    00000111
    00000000
    00000000
    
  • 添加位移后即可正确分配,如下:
    (((1U << (page_count + offset > 8 ? remainder : 0)) - 1) << (8 - remainder))

    00000111
    11100000
    00000000
    
  • 其它说明:

    • 这个bug是基于我的理解给出的,我的理解可能有误
    • 这个bug似乎在目前(内存管理章节,不涉及后续章节)影响并不大甚至是没有影响,并且我稍微看了后面章节的代码,好像pmm这个文件内部已经大变样了,因此我也不清楚后续的代码是否会出现这个问题

A sleeper error.

It will happen at the commitment (hash : 620a2ee). I don't know if the kernel has same error in later edition, because I haven't add "vfs" in my code.You can try in you last edition.

If we run the code like this:

void __USER__ _lxinit_main() {

  pid_t pid;
  for(size_t i=0; i<7; ++i){
    if(!(pid=fork())){
      while(1){
        sleep(1);
        kprintf("%d\n", i);
      }
    }
  }
  spin();
}

The endless loop will happen in function(check_sleepers).

I did some modifications in my code:

void check_sleepers(){

    struct task_struct *init_task = sched_ctx.tasks + 1;
    struct task_struct *pos, *n;
    time_t now = clock_systime();

    list_for_each(pos, n, &init_task->timer.sleepers, timer.sleepers){

        if(TASK_TERMINATED(pos->state)){
            goto __delete_slp;
        }

        time_t *wtim = &pos->timer.wakeup;
        time_t *atim = &pos->timer.alarm;

        if(*wtim && now >= *wtim){
            *wtim      = 0;
            pos->state = TASK_STOPPED;
        }

        if(*atim && now >= *atim){
            *atim = 0;
        }

        if(!*wtim && !*atim){
    __delete_slp:
            list_delete(&pos->timer.sleepers);
        }
    }

}

It could solve this problem preliminarily. I'm not sure if it has other errors.

“i686-elf-gcc:未找到命令”

折腾了两天,实则是没办法了,,,
运行在virtualbox上,系统为Ubuntu-20.04
手动编译失败,执行up给的脚本也失败

提供一个工具下载脚本

Hi, 你好, 我是从Bilibili follow来的, 前几天有在评论区问过系统引导的问题. 因为我自己也在写操作系统, 所以刚好写了一个脚本用于下载编译和安装交叉编译工具链的, 包括: bochs (sorry, 因为我不太会用qemu, 未来学习了可能会update一下), nasm, binutils, gcc.

源代码下载, 编译和安装目录会在脚本所在的文件夹下创建一个tools文件夹

使用起来还是很方便的, 基本能做到一键下载, 编译和安装 (Sorry我只有一台x86机器, 没有在别的平台和系统上测过). 希望可以采纳一下

#! /bin/bash

shell_folder=$(cd "$(dirname "$0")" || exit; pwd)
export PREFIX="$shell_folder"/tools
export TARGET=i686-elf
export PATH="$PREFIX/bin:$PATH"
export BXSHARE="$PREFIX/share/bochs"

purple="\e[35m"
green="\e[32m"
red="\e[31m"
return='\e[0m'


if [[ $1 = '-h' ]] || [[ $1 = '--help' ]]; then
    echo "Init tools for downloading, compiling, and installing debugger, cross-compile tool-chain for OS development"
    echo 'Options:'
    echo '    -h, --help                Show this help message'
    echo '    -d, --download            Download toolchain'
    echo '    -c, --compile             Compile toolchain'
fi

if [[ $1 = "-d" ]] || [[ $1 = "--download" ]] || [[ ! -d "$shell_folder"/tools ]]; then
    if [[ ! -d "$shell_folder"/tools/src ]]; then
        read -p 'tools/src not exits, download? <y/n>: ' download
        if [[ $download = "y" ]]; then
            mkdir -p "$shell_folder"/tools/src
        fi
    fi
    if [[ $1 = "-d" ]] || [[ $1 = "--download" ]]; then
        echo 'Downloading debug tool'
        echo -e "$purple=> bochs-2.7$return" 
        if  [ -f "$shell_folder"/tools/src/bochs-2.7.tar.gz ]; then
            echo -e "${green}bochs download success$return"
        else
            if wget -t 5 -T 5 -c --quiet --show-progress -O "$shell_folder"/tools/src/bochs-2.7.tar.gz  https://sourceforge.net/projects/bochs/files/bochs/2.7/bochs-2.7.tar.gz ; then
                echo -e "${green}bochs download success$return"
            else
                echo -e "${red}bochs download fail, exiting...$return"
            fi
        fi
        echo 'Downloading cross-compiler...'
        echo -e "$purple=> gcc-10.4$return"
        if  wget -T 5 -c --quiet --show-progress -P "$shell_folder"/tools/src https://ftp.gnu.org/gnu/gcc/gcc-10.4.0/gcc-10.4.0.tar.gz; then
            echo -e "${green}gcc download success$return"
        else
            echo -e  "${red}gcc download fail, exiting...${return}" && exit
        fi
        echo -e "$purple=> binutils-2.7$return"
        if  wget -c -T 5 --quiet --show-progress -P "$shell_folder"/tools/src https://ftp.gnu.org/gnu/binutils/binutils-2.38.tar.gz;then
            echo -e "${green}binutils download success$return"
        else
            echo -e  "${green}binutils download fail, exiting...$return" && exit
        fi
    fi
fi

if [[ $1 = "-c" ]] || [[ $1 = "--compile" ]]; then
    log="$shell_folder"/tools/log
    mkdir -p "$log"
    echo -e "Target platform $green$TARGET${return}"
    echo -e "Cross-compile tools will be installed: ${green}$PREFIX$return"
    echo -e "Compile logs will be written to ${green}$log$return"
    echo "Compile may take 10 minutes, start in 3 seconds..."
    sleep 3s
    # bochs
    echo -e "$purple=> Compile bochs-2.7: no gdb$return"
    cd "$shell_folder"/tools/src || (echo "cd to tools/src fail, nothong changed, exiting..." && exit)
    echo "Extracting..."
    sleep 2s
    tar xzf "$shell_folder"/tools/src/bochs-2.7.tar.gz\
    && mkdir -p build-bochs\
    && cd "$shell_folder"/tools/src/build-bochs\
    && (../bochs-2.7/configure --prefix="$PREFIX" --enable-debugger --enable-iodebug --enable-x86-64-debugger --with-x --with-x11 2>&1 | tee "$log"/bochs-debugger-configure.log)\
    && (make -j "$(nproc)" 2>&1 | tee "$log"/bochs-debugger-make.log)\
    && (make install -j "$(nproc)" 2>&1 | tee "$log"/bochs-debugger-make-install.log)\
    && (../bochs-2.7/configure --prefix="$shell_folder"/tools/build-bochs-gdb --enable-gdb-stub --enable-iodebug --enable-x86-64-debugger --with-x --with-x11 2>&1 | tee "$log"/bochs-gdb-debugger-configure.log)\
    && (make -j "$(nproc)" 2>&1 | tee "$log"/bochs-gdb-make.log)\
    && (make install -j "$(nproc)" 2>&1 | tee "$log"/bochs-gdb-make-install.log)\
    && (mv "$shell_folder"/tools/build-bochs-gdb/bin/bochs "$shell_folder"/tools/bin/bochs-gdb)\
    && rm -r "$shell_folder"/tools/build-bochs-gdb
    echo -e "${green}bochs, bochs-gdb compile and install successfully$return, PS: ignore bochsdgb not found, it's no an error"

    # binutils
    echo -e "$purple=> Compile binutils.2.38$return"
    cd "$shell_folder"/tools/src || (echo "cd to tools/src fail, nothong changed, exiting..." && exit)
    echo "Extracting..."
    sleep 2s
    tar xzf "$shell_folder"/tools/src/binutils-2.38.tar.gz\
    && mkdir -p build-binutils\
    && cd "$shell_folder"/tools/src/build-binutils\
    && (../binutils-2.38/configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror 2>&1 | tee "$log"/binutil-configure.log)\
    && (make -j "$(nproc)" 2>&1 | tee "$log"/binutil-make.log)\
    && (make install 2>&1 | tee "$log"/binutil-make-install.log)
    echo -e "${green}binutils compile and install successfully${return}"

    # gcc
    echo -e "$purple=> Compile gcc$return"
    cd "$shell_folder"/tools/src || (echo "cd to tools fail, nothong changed, exiting..." && exit)
    echo "search $TARGET-as..."
    which -- $TARGET-as || (echo "$TARGET-as is not in the PATH, aborting..."; exit)
    echo "Extracting..."
    sleep 2s
    tar xzf "$shell_folder"/tools/src/gcc-10.4.0.tar.gz\
    && mkdir -p build-gcc\
    && cd "$shell_folder"/tools/src/build-gcc || exit\
    && ../gcc-10.4.0/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-language=c,c++ --without-headers 2>&1 | tee "$log"/gcc-configure.log\
    && make -j "$(nproc)" all-gcc 2>&1 | tee "$log"/gcc-make-all-gcc.log\
    && make -j "$(nproc)" all-target-libgcc 2>&1 | tee "$log"/gcc-make-all-target-libgcc.log\
    && make install-gcc 2>&1 | tee "$log"/gcc-make-install-gcc.log\
    && make install-target-libgcc 2>&1 | tee "$log"/gcc-make-install-target-libgcc.log
    echo -e "${green}gcc compile and install successfully$return"

    cd "$shell_folder" || exit;
    echo "You can run bochs, bochs-gdb, $TARGET-gcc, $TARGET-ld, $TARGET-as now"
    echo -e "${green}"
    echo 'Run `source init.sh` first or manually add tools/bin into your PATH'
    echo -e "${return}"
fi

LOW VERSION gcc in all-build.sh

lunaix-os/slides/c0-workspace/all-build.sh will install a lower version of gcc. So the project doesn't build properly.

solution

We could keep the process of building qemu in all-build.sh.Running bochs-build.sh and gcc-build.sh to build other tools.

如何解决版本不一致问题

就是我在看你的教程的过程中,发现一个比较大的问题,就比如你的前期写vga那段的makefile文件,因为比较长,然后你就跳过了吗,对于这种跳过的文件,我一般都是较短的话就暂停,然后照着一个一个打,但是那种较长的,比如哪个makefile文件。你的视频中当时是有56行左右,然后出现的时间也不长,我也不好暂停抄,然后想着来github复制一下,发现你的是最终版了,我复制下去运行一下就是报错,因为还有很多东西我都还没跟上来,这个只能自己把makefile给学了,然后自己手撸吗?还是说暂时跳过这个,等到了最后面的时候才执行吗?

out of bound write in __init_pile

pile_name array' length is PILE_NAME_MAXLEN

struct cake_pile
{
    struct llist_header piles;
    struct llist_header full;
    struct llist_header partial;
    struct llist_header free;
    u32_t offset;
    u32_t piece_size;
    u32_t cakes_count;
    u32_t alloced_pieces;
    u32_t pieces_per_cake;
    u32_t pg_per_cake;
    char pile_name[PILE_NAME_MAXLEN];

    pile_cb ctor;
};

strncpy will write PILE_NAME_MAXLEN+1 bytes

void
__init_pile(struct cake_pile* pile,
            char* name,
            unsigned int piece_size,
            unsigned int pg_per_cake,
            int options)
{
    //...
    strncpy(pile->pile_name, name, PILE_NAME_MAXLEN);
    //...
}

This is test code

#include <stdio.h>
#include <stdlib.h>

#define PILE_NAME_MAXLEN 20

struct cake_pile
{
    // struct llist_header piles;
    // struct llist_header full;
    // struct llist_header partial;
    // struct llist_header free;
    // u32_t offset;
    // u32_t piece_size;
    // u32_t cakes_count;
    // u32_t alloced_pieces;
    // u32_t pieces_per_cake;
    // u32_t pg_per_cake;
    char pile_name[PILE_NAME_MAXLEN+10];

    // pile_cb ctor;
};

char*
strncpy(char* dest, const char* src, unsigned long n)
{
    char c;
    unsigned int i = 0;
    while ((c = src[i]) && i <= n)
        dest[i++] = c;
    while (i <= n)
        dest[i++] = 0;
    return dest;
}


int main()
{
    struct cake_pile pile;
    char *str = "a";
    memset(pile.pile_name, 0xaa, 30);
    strncpy(pile.pile_name, str, PILE_NAME_MAXLEN);
    for (size_t i = 0; i < 30; i++)
    {
        printf("0x%hhx ", pile.pile_name[i]);
    }
    printf("\n");
    
    return 0;
}

Broken link to this repo from your blog

Hi Lunaixsky,

I noticed that the link to this repo from your blog is not working. On your blog project page, the link for LunaixOS is https://github.com/Minep/LunaixOS, which should be updated to https://github.com/Minep/lunaix-os 🤔. Hope this could be helpful!

Thanks for your great work!

Tony

some bugs in operations of hlist

struct hlist_node
{
    struct hlist_node *next, **pprev;
};

static inline void
hlist_delete(struct hlist_node* node)
{
    if (!node->pprev)
        return;
    // 将前一个node指向后一个node
    *node->pprev = node->next;
    // 没有将后一个node指向前一个node
    node->next = 0;
    node->pprev = 0;
}

static inline void
hlist_add(struct hlist_node** head, struct hlist_node* node)
{
    // 该函数也存在问题
    node->next = *head;
    if (*head) {
        (*head)->pprev = &node->next;
    }
    node->pprev = head;
    *head = node;
}

关于lib函数的一点疑问

请问lib中有些函数用纯c,有些使用汇编是什么原因?
举例:

  1. memcpy: asm volatile("movl %1, %%edi\n"
    "rep movsb\n" ::"S"(src),
    "r"(dest),
    "c"(num)
    : "edi", "memory");
    其中rep指令的使用看得不是很明白、

  2. 关于memmove为何使用u8_t的类型?

看源码的时候产生了一些困惑,期待您的解答。

Error: unsupported instruction `mov' when running "make all"

Full Error Message

$ make all    
 BUILD: arch/x86/boot.S
arch/x86/boot.S: Assembler messages:
arch/x86/boot.S:78: Error: unsupported instruction `mov'
arch/x86/boot.S:80: Error: unsupported instruction `mov'
arch/x86/boot.S:84: Error: unsupported instruction `mov'
arch/x86/boot.S:86: Error: unsupported instruction `mov'
arch/x86/boot.S:88: Error: unsupported instruction `mov'
arch/x86/boot.S:94: Error: invalid instruction suffix for `push'
make: *** [makefile:24: build/obj/arch/x86/boot.S.o] Error 1

gcc,binutils Version

$ i686-elf-gcc -dumpmachine
i686-elf
$ i686-elf-gcc --version
i686-elf-gcc (GCC) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ i686-elf-as --version
GNU assembler (GNU Binutils) 2.39
Copyright (C) 2022 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or later.
This program has absolutely no warranty.
This assembler was configured for a target of `i686-elf'.

System Info

OS: Arch Linux x86_64 
Host: HP EliteBook 840 G3 
Kernel: 6.0.1-arch2-1 
Uptime: 8 hours, 9 mins 
Packages: 1123 (pacman) 
Shell: zsh 5.9 
Resolution: 1920x1080 
DE: Plasma 5.26.0 
WM: KWin 
Terminal: yakuake 
CPU: Intel i5-6200U (4) @ 2.800GHz 
GPU: Intel Skylake GT2 [HD Graphics 520] 
Memory: 4298MiB / 15883MiB 

我猜测是binutils-2.39不再支持类似的指令了?请问我需要改用低版本的gcc和binutils吗?

工具链问题

你能不能用i386或者i486工具链编译重新构建一下
比如用musl库,毕竟libc才能编译一些linux软件过来,方便很多的

Side-effect-free infinity-loop is undefined behaviour in C

WG14/N1528
C23 standard(WG14/N3096,ISO/IEC 9899:2023)
According to ISO/IEC 9899:2023 6.8.5,

An iteration statement may be assumed by the implementation to terminate if its controlling
expression is not a constant expression200), and none of the following operations are performed in its body, controlling expression or (in the case of a for statement) its expression-3201) :

  1. This is intended to allow compiler transformations such as removal of empty loops even when termination cannot be proven.

simply while (1); can be assumed by compilers to terminate.

其实就是我写的时候开了O3被clang编译没了直接int3了


Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.