Import very modest Userland.

This commit is contained in:
Andreas Kling 2018-10-22 14:06:22 +02:00
parent 4cbf079a17
commit 63764b3a65
Notes: sideshowbarker 2024-07-19 18:45:13 +09:00
23 changed files with 81 additions and 27 deletions

View file

@ -1,6 +1,6 @@
#pragma once
#ifdef SERENITY_KERNEL
#ifdef SERENITY
#include "kassert.h"
#else
#include <assert.h>

View file

@ -1,6 +1,6 @@
#pragma once
#ifdef SERENITY_KERNEL
#ifdef SERENITY
#include <Kernel/StdLib.h>
#else
#include <cstring>

View file

@ -1,6 +1,6 @@
#pragma once
#if defined(SERENITY_KERNEL) || defined(SERENITY_LIBC)
#if defined(SERENITY)
typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned int dword;

View file

@ -1,6 +1,6 @@
#pragma once
#ifdef SERENITY_KERNEL
#ifdef SERENITY
#include <Kernel/kmalloc.h>
#else
#include <new>

View file

@ -1,6 +1,6 @@
#pragma once
#ifdef SERENITY_KERNEL
#ifdef SERENITY
#include <Kernel/kstdio.h>
#else
#include <cstdio>

View file

@ -1,6 +1,6 @@
#pragma once
#ifdef SERENITY_KERNEL
#ifdef SERENITY
#include <Kernel/ktime.h>
#else
#include <time.h>

View file

@ -1,7 +1,7 @@
#include "ELFImage.h"
#include <AK/kstdio.h>
#ifdef SERENITY_KERNEL
#ifdef SERENITY
ELFImage::ELFImage(ByteBuffer&& buffer)
: m_buffer(buffer)
{
@ -136,7 +136,7 @@ const char* ELFImage::tableString(unsigned offset) const
const char* ELFImage::rawData(unsigned offset) const
{
#ifdef SERENITY_KERNEL
#ifdef SERENITY
return reinterpret_cast<const char*>(m_buffer.pointer()) + offset;
#else
return reinterpret_cast<const char*>(m_file.pointer()) + offset;

View file

@ -1,6 +1,6 @@
#pragma once
#ifndef SERENITY_KERNEL
#ifndef SERENITY
#include <AK/MappedFile.h>
#endif
@ -11,7 +11,7 @@
class ELFImage {
public:
#ifdef SERENITY_KERNEL
#ifdef SERENITY
explicit ELFImage(ByteBuffer&&);
#else
explicit ELFImage(MappedFile&&);
@ -131,7 +131,7 @@ private:
const char* sectionHeaderTableString(unsigned offset) const;
const char* sectionIndexToString(unsigned index);
#ifdef SERENITY_KERNEL
#ifdef SERENITY
ByteBuffer m_buffer;
#else
MappedFile m_file;

View file

@ -1,7 +1,7 @@
#include "ELFLoader.h"
#include <AK/kstdio.h>
#ifdef SERENITY_KERNEL
#ifdef SERENITY
ELFLoader::ELFLoader(ExecSpace& execSpace, ByteBuffer&& file)
#else
ELFLoader::ELFLoader(ExecSpace& execSpace, MappedFile&& file)

View file

@ -9,7 +9,7 @@
class ELFLoader {
public:
#ifdef SERENITY_KERNEL
#ifdef SERENITY
ELFLoader(ExecSpace&, ByteBuffer&&);
#else
ELFLoader(ExecSpace&, MappedFile&&);

View file

@ -12,7 +12,7 @@ ExecSpace::~ExecSpace()
{
}
#ifdef SERENITY_KERNEL
#ifdef SERENITY
int puts(const char* str)
{
kprintf("%s\n", str);
@ -25,7 +25,7 @@ void ExecSpace::initializeBuiltins()
m_symbols.set("puts", { (char*)puts, 0 });
}
#ifdef SERENITY_KERNEL
#ifdef SERENITY
bool ExecSpace::loadELF(ByteBuffer&& file)
#else
bool ExecSpace::loadELF(MappedFile&& file)
@ -49,7 +49,7 @@ static void disassemble(const char* data, size_t length)
if (!length)
return;
#ifdef SERENITY_KERNEL
#ifdef SERENITY
for (unsigned i = 0; i < length; ++i) {
kprintf("%b ", (unsigned char)data[i]);
}

View file

@ -37,7 +37,7 @@ public:
ExecSpace();
~ExecSpace();
#ifdef SERENITY_KERNEL
#ifdef SERENITY
bool loadELF(ByteBuffer&&);
#else
bool loadELF(MappedFile&&);

View file

@ -61,7 +61,7 @@ FLAVOR_FLAGS = -fomit-frame-pointer -mregparm=3 -march=i386 -m32 -fno-exceptions
OPTIMIZATION_FLAGS = -Os -fno-asynchronous-unwind-tables
INCLUDE_FLAGS = -I.. -I.
DEFINES = -DSERENITY_KERNEL -DSANITIZE_PTRS
DEFINES = -DSERENITY -DSANITIZE_PTRS
CXXFLAGS = $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(KERNEL_FLAGS) $(FLAVOR_FLAGS) $(ARCH_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
#CXX = /usr/local/gcc-4.8.1-for-linux64/bin/x86_64-pc-linux-g++

2
LibC/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*.o
LibC.a

View file

@ -1,6 +1,7 @@
OBJS = \
stdio.o \
unistd.o
unistd.o \
entry.o
LIBRARY = LibC.a
ARCH_FLAGS =
@ -11,7 +12,7 @@ FLAVOR_FLAGS = -fomit-frame-pointer -mregparm=3 -march=i386 -m32 -fno-exceptions
OPTIMIZATION_FLAGS = -Os -fno-asynchronous-unwind-tables
INCLUDE_FLAGS = -I.. -I.
DEFINES = -DSERENITY_LIBC -DSANITIZE_PTRS
DEFINES = -DSERENITY -DSANITIZE_PTRS
CXXFLAGS = $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(LIBC_FLAGS) $(FLAVOR_FLAGS) $(ARCH_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
CXX = g++

12
LibC/entry.cpp Normal file
View file

@ -0,0 +1,12 @@
#include <Kernel/Syscall.h>
extern "C" int main(int, char**);
extern "C" int elf_entry()
{
// FIXME: Pass appropriate argc/argv.
main(0, nullptr);
// Birger's birthday <3
return 20150614;
}

27
Userland/Makefile Normal file
View file

@ -0,0 +1,27 @@
OBJS = \
id.o
ARCH_FLAGS =
STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib
USERLAND_FLAGS = -ffreestanding -fno-stack-protector -fno-ident
WARNING_FLAGS = -Wextra -Wall -Wundef -Wcast-qual -Wwrite-strings
FLAVOR_FLAGS = -fomit-frame-pointer -mregparm=3 -march=i386 -m32 -fno-exceptions -fno-rtti -ffunction-sections -fdata-sections -fmerge-all-constants -fno-unroll-loops -falign-functions=1 -falign-jumps=1 -falign-loops=1 -fno-pie -fno-pic
OPTIMIZATION_FLAGS = -Os -fno-asynchronous-unwind-tables
INCLUDE_FLAGS = -I.. -I.
DEFINES = -DSERENITY -DSANITIZE_PTRS
CXXFLAGS = $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(USERLAND_FLAGS) $(FLAVOR_FLAGS) $(ARCH_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
CXX = g++
LD = ld
AR = ar
LDFLAGS = -T linker.ld --strip-debug -melf_i386 --gc-sections --build-id=none -z norelro -z now
all: $(OBJS)
.cpp.o:
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<
clean:
@echo "CLEAN"; rm -f $(LIBRARY) $(OBJS)

12
Userland/id.cpp Normal file
View file

@ -0,0 +1,12 @@
#include <LibC/unistd.h>
#include <LibC/stdio.h>
int main(int c, char** v)
{
uid_t uid = getuid();
gid_t gid = getgid();
pid_t pid = getpid();
printf("uid=%u, gid=%u, pid=%u\n", uid, gid, pid);
return 0;
}

View file

@ -3,7 +3,7 @@
#include <AK/Retainable.h>
#include <AK/Types.h>
#ifdef SERENITY_KERNEL
#ifdef SERENITY
// FIXME: Support 64-bit DiskOffset
typedef dword DiskOffset;
#else

View file

@ -13,7 +13,7 @@ FileHandle::~FileHandle()
{
}
#ifndef SERENITY_KERNEL
#ifndef SERENITY
bool additionWouldOverflow(Unix::off_t a, Unix::off_t b)
{
ASSERT(a > 0);
@ -69,7 +69,7 @@ Unix::off_t FileHandle::seek(Unix::off_t offset, int whence)
break;
case SEEK_CUR:
newOffset = m_currentOffset + offset;
#ifndef SERENITY_KERNEL
#ifndef SERENITY
if (additionWouldOverflow(m_currentOffset, offset))
return -EOVERFLOW;
#endif

View file

@ -14,7 +14,7 @@ public:
ByteBuffer readEntireFile();
#ifdef SERENITY_KERNEL
#ifdef SERENITY
int fd() const { return m_fd; }
void setFD(int fd) { m_fd = fd; }
#endif
@ -26,7 +26,7 @@ private:
Unix::off_t m_currentOffset { 0 };
#ifdef SERENITY_KERNEL
#ifdef SERENITY
int m_fd { -1 };
#endif
};

View file

@ -2,7 +2,7 @@
#include "UnixTypes.h"
#ifdef SERENITY_KERNEL
#ifdef SERENITY
inline static const Unix::off_t maxFileOffset = 2147483647;
#else
#include <limits>

View file

@ -15,7 +15,7 @@ typedef dword nlink_t;
typedef dword uid_t;
typedef dword gid_t;
#ifdef SERENITY_KERNEL
#ifdef SERENITY
// FIXME: Support 64-bit offsets!
typedef signed_dword off_t;
typedef unsigned int time_t;