<commit_before>/* Copyright (c) 2016, 2017, 2018, 2019, 2020 <NAME>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* kernel/src/kernel.cpp
 * The kernel's main function.
 */

#include <assert.h>
#include <string.h>
#include <dennix/fcntl.h>
#include <dennix/kernel/addressspace.h>
#include <dennix/kernel/devices.h>
#include <dennix/kernel/directory.h>
#include <dennix/kernel/file.h>
#include <dennix/kernel/initrd.h>
#include <dennix/kernel/log.h>
#include <dennix/kernel/panic.h>
#include <dennix/kernel/pci.h>
#include <dennix/kernel/physicalmemory.h>
#include <dennix/kernel/pit.h>
#include <dennix/kernel/process.h>
#include <dennix/kernel/ps2.h>
#include <dennix/kernel/rtc.h>
#include <dennix/kernel/terminal.h>

#ifndef DENNIX_VERSION
#  define DENNIX_VERSION ""
#endif

static Reference<DirectoryVnode> loadInitrd(multiboot_info* multiboot);

static multiboot_info multiboot;

extern "C" void kmain(uint32_t /*magic*/, paddr_t multibootAddress) {
    AddressSpace::initialize();

    // Copy the multiboot structure. This cannot fail because we just freed some
    // memory.
    vaddr_t multibootMapping;
    size_t mapSize;
    const multiboot_info* multibootMapped = (const multiboot_info*)
            kernelSpace->mapUnaligned(multibootAddress, sizeof(multiboot_info),
            PROT_READ, multibootMapping, mapSize);
    memcpy(&multiboot, multibootMapped, sizeof(multiboot_info));
    kernelSpace->unmapPhysical(multibootMapping, mapSize);

    Log::earlyInitialize(&multiboot);
    // Kernel panic works after this point.

    PhysicalMemory::initialize(&multiboot);

    Log::initialize();
    Log::printf("Welcome to Dennix " DENNIX_VERSION "\n");
    Log::printf("Initializing PS/2 Controller...\n");
    PS2::initialize();

    Thread::initializeIdleThread();
    Interrupts::initPic();
    Log::printf("Initializing RTC and PIT...\n");
    Rtc::initialize();
    Pit::initialize();

    Log::printf("Scanning for PCI devices...\n");
    Pci::scanForDevices();

    Log::printf("Enabling interrupts...\n");
    Interrupts::enable();

    // Load the initrd.
    Log::printf("Loading Initrd...\n");
    Reference<DirectoryVnode> rootDir = loadInitrd(&multiboot);
    if (!rootDir) PANIC("Could not load initrd");
    Reference<FileDescription> rootFd = xnew FileDescription(rootDir, O_SEARCH);
    Process::current()->rootFd = rootFd;

    Devices::initialize(rootDir);
    rootDir->mkdir("tmp", 0777);

    Log::printf("Starting init process...\n");
    Reference<Vnode> program = resolvePath(rootDir, "/sbin/init");
    if (!program) PANIC("No init program found");

    Process* initProcess = xnew Process();
    const char* argv[] = { "init", nullptr };
    const char* envp[] = { nullptr };
    if (initProcess->execute(program, (char**) argv, (char**) envp) < 0) {
        PANIC("Failed to start init process");
    }
    program = nullptr;
    if (!Process::addProcess(initProcess)) {
        PANIC("Failed to start init process");
    }
    assert(initProcess->pid == 1);
    Process::initProcess = initProcess;

    initProcess->controllingTerminal = terminal;
    Reference<FileDescription> descr = xnew FileDescription(terminal, O_RDWR);
    initProcess->addFileDescriptor(descr, 0); // stdin
    initProcess->addFileDescriptor(descr, 0); // stdout
    initProcess->addFileDescriptor(descr, 0); // stderr

    initProcess->rootFd = rootFd;
    initProcess->cwdFd = rootFd;<commit_msg>Add /run directory for runtime files.<commit_after>/* Copyright (c) 2016, 2017, 2018, 2019, 2020 <NAME>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* kernel/src/kernel.cpp
 * The kernel's main function.
 */

#include <assert.h>
#include <string.h>
#include <dennix/fcntl.h>
#include <dennix/kernel/addressspace.h>
#include <dennix/kernel/devices.h>
#include <dennix/kernel/directory.h>
#include <dennix/kernel/file.h>
#include <dennix/kernel/initrd.h>
#include <dennix/kernel/log.h>
#include <dennix/kernel/panic.h>
#include <dennix/kernel/pci.h>
#include <dennix/kernel/physicalmemory.h>
#include <dennix/kernel/pit.h>
#include <dennix/kernel/process.h>
#include <dennix/kernel/ps2.h>
#include <dennix/kernel/rtc.h>
#include <dennix/kernel/terminal.h>

#ifndef DENNIX_VERSION
#  define DENNIX_VERSION ""
#endif

static Reference<DirectoryVnode> loadInitrd(multiboot_info* multiboot);

static multiboot_info multiboot;

extern "C" void kmain(uint32_t /*magic*/, paddr_t multibootAddress) {
    AddressSpace::initialize();

    // Copy the multiboot structure. This cannot fail because we just freed some
    // memory.
    vaddr_t multibootMapping;
    size_t mapSize;
    const multiboot_info* multibootMapped = (const multiboot_info*)
            kernelSpace->mapUnaligned(multibootAddress, sizeof(multiboot_info),
            PROT_READ, multibootMapping, mapSize);
    memcpy(&multiboot, multibootMapped, sizeof(multiboot_info));
    kernelSpace->unmapPhysical(multibootMapping, mapSize);

    Log::earlyInitialize(&multiboot);
    // Kernel panic works after this point.

    PhysicalMemory::initialize(&multiboot);

    Log::initialize();
    Log::printf("Welcome to Dennix " DENNIX_VERSION "\n");
    Log::printf("Initializing PS/2 Controller...\n");
    PS2::initialize();

    Thread::initializeIdleThread();
    Interrupts::initPic();
    Log::printf("Initializing RTC and PIT...\n");
    Rtc::initialize();
    Pit::initialize();

    Log::printf("Scanning for PCI devices...\n");
    Pci::scanForDevices();

    Log::printf("Enabling interrupts...\n");
    Interrupts::enable();

    // Load the initrd.
    Log::printf("Loading Initrd...\n");
    Reference<DirectoryVnode> rootDir = loadInitrd(&multiboot);
    if (!rootDir) PANIC("Could not load initrd");
    Reference<FileDescription> rootFd = xnew FileDescription(rootDir, O_SEARCH);
    Process::current()->rootFd = rootFd;

    Devices::initialize(rootDir);
    rootDir->mkdir("tmp", 0777);
    rootDir->mkdir("run", 0755);

    Log::printf("Starting init process...\n");
    Reference<Vnode> program = resolvePath(rootDir, "/sbin/init");
    if (!program) PANIC("No init program found");

    Process* initProcess = xnew Process();
    const char* argv[] = { "init", nullptr };
    const char* envp[] = { nullptr };
    if (initProcess->execute(program, (char**) argv, (char**) envp) < 0) {
        PANIC("Failed to start init process");
    }
    program = nullptr;
    if (!Process::addProcess(initProcess)) {
        PANIC("Failed to start init process");
    }
    assert(initProcess->pid == 1);
    Process::initProcess = initProcess;

    initProcess->controllingTerminal = terminal;
    Reference<FileDescription> descr = xnew FileDescription(terminal, O_RDWR);
    initProcess->addFileDescriptor(descr, 0); // stdin
    initProcess->addFileDescriptor(descr, 0); // stdout
    initProcess->addFileDescriptor(descr, 0); // stderr

    initProcess->rootFd = rootFd;
    initProcess->cwdFd = rootFd;
{'fraction_non_alphanumeric': 0.12906344410876133, 'fraction_numerical': 0.008700906344410876, 'mean_word_length': 4.182216656230432, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1164482', 'n_tokens_mistral': 2709, 'n_tokens_neox': 2531, 'n_words': 688}