1

add vorgabe03 + initial bump allocator

This commit is contained in:
churl
2022-05-06 11:28:20 +02:00
parent ab7a9c2ecb
commit 4ee0b701d7
17 changed files with 472 additions and 59 deletions

0
.gitignore vendored Executable file → Normal file
View File

View File

@ -62,10 +62,10 @@ VERBOSE = @
ASMOBJFORMAT = elf
OBJDIR = ./build
DEPDIR = ./dep
DRIVE_FD = /dev/sdb
DRIVE_FD = /media/student/LUCAS
# ACHTUNG: ein falsch angegebenes Laufwerk kann dazu fuehren, dass Daten auf dem
# spezifizierten Laufwerk verloren gehen! Nicht mit root-Rechten ausfuehren!
DRIVE_HD = /dev/sdb
DRIVE_HD = /media/student/LUCAS
DELETE = rm
ASM = nasm
CC ?= gcc

83
c_os/kernel/Allocator.cc Normal file
View File

@ -0,0 +1,83 @@
/*****************************************************************************
* *
* A L L O C A T O R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Einfache Speicherverwaltung. 'new' und 'delete' werden *
* durch Ueberladen der entsprechenden Operatoren *
* realisiert. *
* *
* Memory-Laylout *
* *
* boot.asm *
* 0x07c0: Bootsector vom BIOS geladen *
* 0x0060: Boot-Code verschiebt sich hier hin *
* 0x9000: Setup-Code (max. 64K inkl. Stack) vom *
* Bootsector-Code geladen *
* setup.asm *
* 0x1000: System-Code (max. 512K) geladen *
* System-Code *
* 0x100000: System-Code, kopiert nach Umschalten in *
* den Protected Mode kopiert (GRUB kann nur *
* an Adressen >1M laden) *
* Globale Variablen: Direkt nach dem Code liegen die globalen *
* Variablen. *
* Heap: *
* 0x300000: Start-Adresse der Heap-Verwaltung *
* 0x400000: Letzte Adresse des Heaps *
* *
* Achtung: Benötigt einen PC mit mindestens 8 MB RAM! *
* *
* Autor: Michael Schoettner, HHU, 1.3.2022 *
*****************************************************************************/
#include <stddef.h>
#include "kernel/Globals.h"
#include "kernel/Allocator.h"
#define MEM_SIZE_DEF 8*1024*102 // Groesse des Speichers = 8 MB
#define HEAP_START 0x300000 // Startadresse des Heaps
#define HEAP_SIZE 1024*1024 // Default-Groesse des Heaps, falls
// nicht über das BIOS ermittelbar
/*****************************************************************************
* Konstruktor: Allocator::Allocator *
*****************************************************************************/
Allocator::Allocator() {
// Groesse des Hauptspeichers (kann über das BIOS abgefragt werden,
// aber sehr umstaendlich, daher hier fest eingetragen
total_mem = MEM_SIZE_DEF;
heap_start = HEAP_START;
heap_end = HEAP_START + HEAP_SIZE;
heap_size = HEAP_SIZE;
initialized = 1;
}
/*****************************************************************************
* Nachfolgend sind die Operatoren von C++, die wir hier ueberschreiben *
* und entsprechend 'mm_alloc' und 'mm_free' aufrufen. *
*****************************************************************************/
void* operator new ( size_t size ) {
return allocator.alloc(size);
}
void* operator new[]( size_t count ) {
return allocator.alloc(count);
}
void operator delete ( void* ptr ) {
allocator.free(ptr);
}
void operator delete[] ( void* ptr ) {
allocator.free(ptr);
}
void operator delete(void*ptr, unsigned int sz) {
allocator.free(ptr);
}

62
c_os/kernel/Allocator.h Normal file
View File

@ -0,0 +1,62 @@
/*****************************************************************************
* *
* A L L O C A T O R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Einfache Speicherverwaltung. 'new' und 'delete' werden *
* durch Ueberladen der entsprechenden Operatoren *
* realisiert. *
* *
* Memory-Laylout *
* *
* boot.asm *
* 0x07c0: Bootsector vom BIOS geladen *
* 0x0060: Boot-Code verschiebt sich hier hin *
* 0x9000: Setup-Code (max. 64K inkl. Stack) vom *
* Bootsector-Code geladen *
* setup.asm *
* 0x1000: System-Code (max. 512K) geladen *
* System-Code *
* 0x100000: System-Code, kopiert nach Umschalten in *
* den Protected Mode kopiert (GRUB kann nur *
* an Adressen >1M laden) *
* Globale Variablen: Direkt nach dem Code liegen die globalen *
* Variablen. *
* Heap: *
* 0x300000: Start-Adresse der Heap-Verwaltung *
* 0x400000: Letzte Adresse des Heaps *
* *
* Achtung: Benötigt einen PC mit mindestens 4 MB RAM! *
* *
* Autor: Michael Schoettner, HHU, 13.6.2020 *
*****************************************************************************/
#ifndef __Allocator_include__
#define __Allocator_include__
// NOTE: I added this
typedef struct {
unsigned int length;
bool free;
unsigned char* next;
} mem_block_header_t;
class Allocator {
private:
Allocator(Allocator& copy); // Verhindere Kopieren
public:
unsigned int heap_start;
unsigned int heap_end;
unsigned int heap_size;
unsigned int initialized;
Allocator();
virtual void init() = 0;
virtual void dump_free_memory() = 0;
virtual void* alloc(unsigned int req_size) = 0;
virtual void free(void* ptr) = 0;
};
#endif

View File

@ -10,19 +10,25 @@
#ifndef __CPU_include__
#define __CPU_include__
class CPU {
class CPU {
private:
CPU(const CPU& copy); // Verhindere Kopieren
CPU(const CPU &copy); // Verhindere Kopieren
public:
CPU() {}
// Prozessor anhalten
inline void halt () {
asm volatile ( "cli;"
"hlt"
);
}
// Time-Stamp-Counter auslesen
inline unsigned long long int rdtsc() {
unsigned long long int ret;
asm volatile("rdtsc"
: "=A"(ret));
unsigned long long int ret;
asm volatile ( "rdtsc" : "=A"(ret) );
return ret;
}
};

View File

@ -10,7 +10,10 @@
#include "kernel/Globals.h"
CPU cpu; // CPU-spezifische Funktionen
PCSPK pcspk; // PC-Lautsprecher
CGA_Stream kout; // Ausgabe-Strom fuer Kernel
Keyboard kb; // Tastatur
CPU cpu; // CPU-spezifische Funktionen
PCSPK pcspk; // PC-Lautsprecher
CGA_Stream kout; // Ausgabe-Strom fuer Kernel
Keyboard kb; // Tastatur
unsigned int total_mem; // RAM total
BumpAllocator allocator;
//LinkedListAllocator allocator;

View File

@ -13,11 +13,15 @@
#include "devices/CGA_Stream.h"
#include "devices/Keyboard.h"
#include "devices/PCSPK.h"
#include "kernel/allocator/BumpAllocator.h"
#include "kernel/CPU.h"
extern CPU cpu; // CPU-spezifische Funktionen
extern PCSPK pcspk; // PC-Lautsprecher
extern CGA_Stream kout; // Ausgabe-Strom fuer Kernel
extern Keyboard kb; // Tastatur
extern CPU cpu; // CPU-spezifische Funktionen
extern PCSPK pcspk; // PC-Lautsprecher
extern CGA_Stream kout; // Ausgabe-Strom fuer Kernel
extern Keyboard kb; // Tastatur
extern unsigned int total_mem; // RAM total
extern BumpAllocator allocator;
//extern LinkedListAllocator allocator;
#endif

View File

@ -0,0 +1,80 @@
/*****************************************************************************
* *
* B U M P A L L O C A T O R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Eine sehr einfache Heap-Verwaltung, welche freigegebenen *
* Speicher nicht mehr nutzen kann. *
* *
* Autor: Michael Schoettner, HHU, 3.3.2022 *
*****************************************************************************/
#include "kernel/Globals.h"
#include <stddef.h>
// #include "kernel/allocator/LinkedListAllocator.h"
/*****************************************************************************
* Methode: BumpAllocator::init *
*---------------------------------------------------------------------------*
* Beschreibung: BumpAllokartor intitialisieren. *
*****************************************************************************/
void BumpAllocator::init() {
/* Hier muess Code eingefuegt werden */
this->heap_start = 3 * 1024 * 1024; // 3 MB
this->heap_size = 1 * 1024 * 1024; // 1 MB
this->heap_end = this->heap_start + this->heap_size; // 4 MB
this->allocations = 0;
this->next = (unsigned char*)heap_start;
this->initialized = true;
}
/*****************************************************************************
* Methode: BumpAllocator::dump_free_memory *
*---------------------------------------------------------------------------*
* Beschreibung: Ausgabe der Freispeicherinfos. Zu Debuggingzwecken. *
*****************************************************************************/
void BumpAllocator::dump_free_memory() {
/* Hier muess Code eingefuegt werden */
kout << "Freier Speicher:" << endl
<< " Heap Start: " << this->heap_start << ", Heap End: " << this->heap_end << endl
<< " Next: " << this->next << ", Allocations: " << this->allocations << endl;
}
/*****************************************************************************
* Methode: BumpAllocator::alloc *
*---------------------------------------------------------------------------*
* Beschreibung: Einen neuen Speicherblock allozieren. *
*****************************************************************************/
void* BumpAllocator::alloc(unsigned int req_size) {
/* Hier muess Code eingefuegt werden */
if (!this->initialized) {
kout << "Allocator not initialized :(" << endl;
return NULL;
}
if (req_size > this->heap_end - (unsigned int)this->next) {
kout << "More memory requested than available :(" << endl;
return NULL;
}
void* allocated = this->next;
this->next = this->next + req_size;
this->allocations = this->allocations + 1;
return allocated;
}
/*****************************************************************************
* Methode: BumpAllocator::free *
*---------------------------------------------------------------------------*
* Beschreibung: Nicht implementiert. *
*****************************************************************************/
void BumpAllocator::free(void* ptr) {
kout << " mm_free: ptr= " << hex << (unsigned int)ptr << ", not supported" << endl;
}

View File

@ -0,0 +1,34 @@
/*****************************************************************************
* *
* B U M P A L L O C A T O R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Eine sehr einfache Heap-Verwaltung, welche freigegebenen *
* Speicher nicht mehr nutzen kann. *
* *
* Autor: Michael Schoettner, HHU, 3.3.2022 *
*****************************************************************************/
#ifndef __BumpAllocator_include__
#define __BumpAllocator_include__
#include "kernel/Allocator.h"
class BumpAllocator : Allocator {
private:
unsigned char* next;
unsigned int allocations;
BumpAllocator(Allocator& copy); // Verhindere Kopieren
public:
BumpAllocator() {}
void init();
void dump_free_memory();
void* alloc(unsigned int req_size);
void free(void* ptr);
};
#endif

View File

@ -0,0 +1,70 @@
/*****************************************************************************
* *
* L I N K E D L I S T A L L O C A T O R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Einfache Speicherverwaltung, welche den freien Speicher *
* mithilfe einer einfach verketteten Liste verwaltet. *
* *
* Autor: Michael Schoettner, HHU, 13.6.2020 *
*****************************************************************************/
#include <stddef.h>
#include "kernel/Globals.h"
#include "kernel/allocator/LinkedListAllocator.h"
#define HEAP_MIN_FREE_BLOCK_SIZE 64 // min. Groesse eines freien Blocks
/*****************************************************************************
* Methode: LinkedListAllocator::init *
*---------------------------------------------------------------------------*
* Beschreibung: Liste der Freispeicherbloecke intitialisieren. *
* Anker zeigt auf ein Dummy-Element. Danach folgt *
* ein Block der den gesamten freien Speicher umfasst. *
* *
* Wird automatisch aufgerufen, sobald eine Funktion der *
* Speicherverwaltung erstmalig gerufen wird. *
*****************************************************************************/
void LinkedListAllocator::init() {
/* Hier muess Code eingefuegt werden */
}
/*****************************************************************************
* Methode: LinkedListAllocator::dump_free_memory *
*---------------------------------------------------------------------------*
* Beschreibung: Ausgabe der Freispeicherliste. Zu Debuggingzwecken. *
*****************************************************************************/
void LinkedListAllocator::dump_free_memory() {
/* Hier muess Code eingefuegt werden */
}
/*****************************************************************************
* Methode: LinkedListAllocator::alloc *
*---------------------------------------------------------------------------*
* Beschreibung: Einen neuen Speicherblock allozieren. *
*****************************************************************************/
void * LinkedListAllocator::alloc(unsigned int req_size) {
/* Hier muess Code eingefuegt werden */
}
/*****************************************************************************
* Methode: LinkedListAllocator::free *
*---------------------------------------------------------------------------*
* Beschreibung: Einen Speicherblock freigeben. *
*****************************************************************************/
void LinkedListAllocator::free(void *ptr) {
/* Hier muess Code eingefuegt werden */
}

View File

@ -0,0 +1,43 @@
/*****************************************************************************
* *
* L I N K E D L I S T A L L O C A T O R *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Einfache Speicherverwaltung, welche den freien Speicher *
* mithilfe einer einfach verketteten Liste verwaltet. *
* *
* Autor: Michael Schoettner, HHU, 13.6.2020 *
*****************************************************************************/
#ifndef __LinkedListAllocator_include__
#define __LinkedListAllocator_include__
#include "kernel/Allocator.h"
// Format eines freien Blocks
struct free_block {
unsigned int size;
struct free_block *next;
};
class LinkedListAllocator : Allocator {
private:
// freie Bloecke werden verkettet
struct free_block *free_start;
LinkedListAllocator(Allocator &copy); // Verhindere Kopieren
public:
LinkedListAllocator () { }
void init ();
void dump_free_memory ();
void* alloc (unsigned int req_size);
void free (void *ptr);
};
#endif

View File

@ -7,26 +7,27 @@
* nachdem dieser in den Protected Mode geschaltet hat und *
* die GDT und IDT initalisiert hat. *
* *
* Autor: Michael Schoettner, HHU, 15.8.2016 *
* Autor: Michael Schoettner, HHU, 11.11.2016 *
*****************************************************************************/
#include "kernel/Allocator.h"
#include "kernel/Globals.h"
#include "user/KeyboardDemo.h"
#include "user/SoundDemo.h"
#include "user/TextDemo.h"
#include "user/HeapDemo.h"
int main() {
/* Hier muess Code eingefuegt werden */
// Speicherverwaltung initialisieren
allocator.init();
// Bildschirm loeschen.
kout.clear();
// TODO: Startmeldung ausgeben
// Startmeldung ausgeben
kout << "Yo it's booting" << endl;
// text_demo();
// sound_demo();
keyboard_demo();
heap_demo();
while (1);
while (1)
;
return 0;
}

View File

@ -29,7 +29,12 @@ MULTIBOOT_EAX_MAGIC equ 0x2badb002
[GLOBAL startup]
[GLOBAL idt]
[GLOBAL __cxa_pure_virtual]
[GLOBAL _ZdlPv]
; Michael Schoettner:
; Nachfolgender label steht fuer das 'delete', welches jetzt implementiert
; wird. Damit der Linker nicht wegen doppelter Definition "meckert"
; nun auskommentieren!
; [GLOBAL _ZdlPv]
[EXTERN main]
[EXTERN int_disp]

37
c_os/user/HeapDemo.cc Normal file
View File

@ -0,0 +1,37 @@
/*****************************************************************************
* *
* H E A P D E M O *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Demonstration der dynamischen Speicherverwaltung. *
* *
* Autor: Michael Schoettner, HHU, 27.12.2016 *
*****************************************************************************/
#include "user/HeapDemo.h"
#include "kernel/Allocator.h"
#include "kernel/Globals.h"
#include "user/MyObj.h"
// Hilfsfunktion: Auf Return-Taste warten
void waitForReturn() {
/* hier muss Code eingefuegt werden */
Key key;
do {
kb.key_hit();
} while (key.ascii() != '\n');
}
void heap_demo() {
/* hier muss Code eingefuegt werden */
allocator.dump_free_memory();
MyObj* a = new MyObj(5);
allocator.dump_free_memory();
MyObj* b = new MyObj(10);
allocator.dump_free_memory();
}

16
kernel/Globals.cc → c_os/user/HeapDemo.h Executable file → Normal file
View File

@ -1,18 +1,16 @@
/*****************************************************************************
* *
* G L O B A L S *
* H E A P D E M O *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Globale Variablen des Systems. *
* Beschreibung: Demonstration der dynamischen Speicherverwaltung. *
* *
* Autor: Michael Schoettner, 30.7.16 *
* Autor: Michael Schoettner, HHU, 25.9.2016 *
*****************************************************************************/
#include "kernel/Globals.h"
#ifndef __HeapDemo_include__
#define __HeapDemo_include__
CPU cpu; // CPU-spezifische Funktionen
PCSPK pcspk; // PC-Lautsprecher
CGA_Stream kout; // Ausgabe-Strom fuer Kernel
Keyboard kb; // Tastatur
void heap_demo();
#endif

10
c_os/user/MyObj.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef __MyObj_INCLUDE_H_
#define __MyObj_INCLUDE_H_
class MyObj {
public:
MyObj(unsigned int val) : value(val) {};
unsigned int value;
};
#endif

View File

@ -1,23 +0,0 @@
/*****************************************************************************
* *
* G L O B A L S *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Globale Variablen des Systems. *
* *
* Autor: Michael Schoettner, 30.7.16 *
*****************************************************************************/
#ifndef __Globals_include__
#define __Globals_include__
#include "kernel/CPU.h"
#include "devices/PCSPK.h"
#include "devices/CGA_Stream.h"
#include "devices/Keyboard.h"
extern CPU cpu; // CPU-spezifische Funktionen
extern PCSPK pcspk; // PC-Lautsprecher
extern CGA_Stream kout; // Ausgabe-Strom fuer Kernel
extern Keyboard kb; // Tastatur
#endif