Skip to content

Commit

Permalink
[Misc] Some Hexa and Tofita ideas
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyTy committed Feb 10, 2024
1 parent 0b39b3e commit f494057
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 19 deletions.
6 changes: 4 additions & 2 deletions boot/loader/trampoline.asm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
; The Tofita Kernel
; Copyright (C) 2020 Oleh Petrenko
; Copyright (C) 2020-2024 Oleh Petrenko
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -66,7 +66,7 @@ trampolineCR3:
mov rbp, rsp ; Frame

; TODO Reset state
;cld ; Clear direction flag
;cld ; Clear direction flag TODO actually do it
;; Clear registers
;xor rax, rax
;xor rdx, rdx
Expand All @@ -80,6 +80,8 @@ trampolineCR3:

; Enter main [with rcx]
o64 call r9
; TODO times 64 - ($-$$) db 0 ; Padding
; TODO size: dw $-$$ to store a size in the bootloader that you could look at in an asm listing or hexdump. But that takes up 2 bytes.

; COM write
global portOutb
Expand Down
10 changes: 8 additions & 2 deletions boot/uefi/boot.hexa
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// The Tofita Kernel
// Copyright (C) 2022-2023 Oleh Petrenko
// Copyright (C) 2022-2024 Oleh Petrenko
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -145,20 +145,25 @@ fun uefi(imageHandle ByValue<EFI_HANDLE>, systemTable EFI_SYSTEM_TABLE) EFI_STAT
serialPrintln("[[[uefi]]] begin: uefiGetMemoryMap")
drawVerbose("Getting memory map...", framebuffer)
var sizeAlloc UInt64 = (ramdisk.size / pageSize + 1) * pageSize
var efiMemoryMap UefiMemoryMap = new UefiMemoryMap()
var efiMemoryMap UefiMemoryMap = new UefiMemoryMap() // TODO can be just by value on stack
// TODO is 512 x sizeof or just 512
efiMemoryMap.memoryMapSize = sizeOf<EFI_MEMORY_DESCRIPTOR>() * 512u64
// TODO bug efiMemoryMap.memoryMapSize = sizeOf<EFI_MEMORY_DESCRIPTOR>() * 512
// TODO sizeOf<EFI_MEMORY_DESCRIPTOR>() ?
// TODO is 512 enough?
// TODO is this storage destination ok?
// TODO use conventional alloc with alignment at 4k
efiMemoryMap.memoryMap = (ramdisk.base + sizeAlloc) as! EFI_MEMORY_DESCRIPTOR
{
var b = efiMemoryMap.memoryMap as! ArrayPointer<UInt8>
for i in efiMemoryMap.memoryMapSize {
// TODO faster with uint64_t
b[i] = buffa[0]
// TODO zero-fill buffer for EFI memory map -< unify to zeroFill()
}
}

// TODO check for error STATUS_EFI_BUFFER_TOO_SMALL
uefiGetMemoryMap(efiMemoryMap, systemTable)
serialPrintln("[[[uefi]]] done: uefiGetMemoryMap")

Expand Down Expand Up @@ -242,6 +247,7 @@ fun uefi(imageHandle ByValue<EFI_HANDLE>, systemTable EFI_SYSTEM_TABLE) EFI_STAT
serialPrintf("[[[uefi]]] loaded asset 'tofita.exe' %d bytes at %8\n", asset.size, asset.data)

drawVerbose("Allocate memory...", framebuffer)
// TODO how to ensure the memory is preserved by UEFI and memory map is unchanged when Exit B S called?
conventionalBuffer = conventionalAllocateLargest(efiMemoryMap)
serialPrintf("[[[uefi]]] large buffer allocated at %u\n", conventionalBuffer)
conventionalOffset = conventionalBuffer
Expand Down
15 changes: 12 additions & 3 deletions build/main.hexa
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// The Tofita Kernel
// Copyright (C) 2022-2023 Oleh Petrenko
// Copyright (C) 2022-2024 Oleh Petrenko
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -110,6 +110,7 @@ for folder in ['NjRAA', 'Tongue', 'Greentea', 'Teapot'] {
}
}

// TODO remove this
for task in ['tasks'] {
let folder = config.tempFolder + tea

Expand Down Expand Up @@ -170,6 +171,8 @@ for task in tasks {
case 'experimental':
// TODO
case 'init-or-clean':
// TODO check the presence of other repos like Tongue etc at ../
// if they are at ./ than show recommendation
cleanupTask = true
case 'asm':
asmTask = true
Expand All @@ -180,11 +183,13 @@ for task in tasks {
case 'kernel':
kernelTask = true
case 'ramdisk':
//TODO ram-disk or assets
ramDiskTask = true
case 'iso':
isoTask = true
case 'riscv64': // TODO
case 'arm64': // TODO
case 'risc64': // TODO
case 'arm64':
// TODO
// TODO disallow doc comment on non expr
{
/// TODO like this one
Expand Down Expand Up @@ -340,6 +345,7 @@ fun runTasks() {

let outobj = folder + '\\' + dlls[i] + '.' + bit + '_dll.obj'

// TODO output warnings! cause -x is unused
phase2.push(
[
clang + ' ',
Expand Down Expand Up @@ -469,6 +475,9 @@ fun runTasks() {
(
' bit named ' + config.tempFolder + '/' + teaFolder + '/!named.bit.lib '
)
// TODO Hexa: }catch e: Any {} `:`
//try {fs.unlinkSync(config.tempFolder + '/' + teaFolder + '/!\(named).64.lib')}catch e: Any {}

phase2.push(libgen + (gen + items64).replaceAll('named', named).replaceAll('bit', '64'))
phase2.push(libgen + (gen + items32).replaceAll('named', named).replaceAll('bit', '32'))
}
Expand Down
4 changes: 3 additions & 1 deletion devices/amd64/amd64page.hexa
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// The Tofita Kernel
// Copyright (C) 2022-2023 Oleh Petrenko
// Copyright (C) 2022-2024 Oleh Petrenko
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -94,5 +94,7 @@ class PageEntry {
@bits(4) var protectionKey UInt8

/// XD/NX-bit: Instructions are not allowed to be executed at addresses within the page
// TODO make it impossible to map executable memory in higher half after boot AT ALL!
// ^ map with EXEC in boot for kernel code and that's it
@bits(1) var disableExecution UInt8
}
5 changes: 4 additions & 1 deletion devices/amd64/amd64paging.hexa
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// The Tofita Kernel
// Copyright (C) 2022-2023 Oleh Petrenko
// Copyright (C) 2022-2024 Oleh Petrenko
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -20,6 +20,7 @@ let addressBits = 12

// TODO remove this
@aligned(4096) var pml4entries ArrayPointer<ByValue<PageEntry>> = null as! ArrayPointer<ByValue<PageEntry>>
// TODO Hexa must error <Int>: @aligned(4096) var pml4entries VirtualMemoryMap = null as! ArrayPointer<ByValue<Int>>

// TODO combine flags into bit mask
fun mapPage(entry PageEntry, address UInt64, writeAllowed Bool, accessibleByUser Bool) {
Expand Down Expand Up @@ -134,6 +135,7 @@ fun resolveAddr(pml4entries ArrayPointer<ByValue<PageEntry>>, virtualAddr UInt64
// TODO ^ same clang warning
// TODO should accept usermode flag so shared memory and GDI buffers can be mapped out of user scope
// ^ probably should be other way for WoW processes
// TODO mapUserMemory and separate mapKernelMemory
fun mapMemory(pml4entries ArrayPointer<ByValue<PageEntry>>, virtualAddr UInt64, physicalAddr UInt64, pageCount UInt32, accessibleByUser Bool) {
serialPrintln("[paging] mapping memory range")

Expand Down Expand Up @@ -241,6 +243,7 @@ fun allocUserspaceMemory(pml4entries PageEntry, virtualAddr UInt64, pageCount UI
// Does NOT allocate physical memory for the requested range
// Returns 0 if requested pointer is occupied
// TODO rename to findUserMemory or findLowerMemory or findProcessMemory
// TODO fun findUserspaceMemoryAt( isAvailableUserMemoryAt
fun findUserspaceMemory(
pml4entries ArrayPointer<ByValue<PageEntry>>,
virtualAddr UInt64,
Expand Down
4 changes: 2 additions & 2 deletions devices/cpu/amd64.hexa
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// The Tofita Kernel
// Copyright (C) 2022-2023 Oleh Petrenko
// Copyright (C) 2022-2024 Oleh Petrenko
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -100,7 +100,7 @@ declare fun rdmsr(
')
@rename('wrmsr')
declare fun wrmsr(
msr UInt64,
msr UInt64, // TODO enum
value UInt64
) Void

Expand Down
5 changes: 4 additions & 1 deletion devices/cpu/physical.hexa
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// The Tofita Kernel
// Copyright (C) 2022-2023 Oleh Petrenko
// Copyright (C) 2022-2024 Oleh Petrenko
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -247,6 +247,9 @@ class PhysicalAllocator {

i++
}

// TODO Hexa: `meta.moduleName` instead of `"physical"`
// ^ `fun log(text, moduleName = meta.callerModuleName)` `log(x, _)` <- assume default arg value
serialPrintf("[physical] allocatePages current %u\n", current)
serialPrintf("[physical] allocatePages currentAt %u\n", currentAt)
}
Expand Down
3 changes: 2 additions & 1 deletion devices/screen/framebuffer.hexa
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// The Tofita Kernel
// Copyright (C) 2022-2023 Oleh Petrenko
// Copyright (C) 2022-2024 Oleh Petrenko
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -131,6 +131,7 @@ fun drawBitmap32(bitmap Bitmap32, x Int16, y Int16) {
for xx in (xStart as! UInt32)...bitmap.width {
// TODO save scanline check to Bool and evaluate eagerly
if x + xx > Screen.widthMinusOne or y + yy > Screen.heightMinusOne {
// TODO use same concept as yStart but for yEnd
continue
}
Screen.pixels[screenStride + x + xx] = bitmap.pixels[stride + xx]
Expand Down
8 changes: 5 additions & 3 deletions kernel/formats/exe/exe.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -366,21 +366,23 @@ class ExeLoader<SIZE, HEADER> {
// TODO `tofitaStdStub`
// TODO separate placeholders for std call etc
// TODO cache the bytes!
// TODO resolve upfront and cache !!! for faster exe load
if let placeholder = getProcAddress("tofitaFastStub".utf8copy().bytes as! ConstArrayPointer<UInt8>, root) {
func = placeholder.ptr as! SIZE
if Theme.verbose and unresolved < 2 {
quakePrintf("[resolveDllImports] using placeholder for {%s}\n".utf16(), szImportName)
// TODO Hexa: meta.funName instead of "resolveDllImports" text
quakePrintf("[resolveDllImports] using placeholder for %s of %s\n".utf16(), szImportName, szName)
}
if Theme.verbose and unresolved == 2 {
quakePrintf("[resolveDllImports] similar messages will be hidden\n".utf16(), szImportName)
quakePrintf("[resolveDllImports] similar messages will be hidden\n".utf16())
}
} else {
serialPrintf("[resolveDllImports] placeholder for {%s} is unresolved\n", szImportName)
if Theme.verbose and unresolved < 2 {
quakePrintf("[resolveDllImports] placeholder for {%s} is unresolved\n".utf16(), szImportName)
}
if Theme.verbose and unresolved == 2 {
quakePrintf("[resolveDllImports] similar messages will be hidden\n".utf16(), szImportName)
quakePrintf("[resolveDllImports] similar messages will be hidden\n".utf16())
}
}

Expand Down
3 changes: 2 additions & 1 deletion kernel/gui/compositor.hexa
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// The Tofita Kernel
// Copyright (C) 2022-2023 Oleh Petrenko
// Copyright (C) 2022-2024 Oleh Petrenko
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -345,6 +345,7 @@ fun initializeCompositor() {
getRamDiskAsset(e, "root/Windows/Resources/Icons/256_image-x-generic.ico")
picture48 = loadIcon(e.data as! ConstArrayPointer<UInt8>, 48)

// TODO `getRamDiskAsset` should be null-safe!!! cause forgot to add asset sometimes
let asset = new RamDiskAsset()
getRamDiskAsset(asset, "root/Windows/Resources/Icons/Beautiful/app32.png")
app32 = loadPng32(asset)
Expand Down
4 changes: 2 additions & 2 deletions kernel/tofita.hexa
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// The Tofita Kernel
// Copyright (C) 2022-2023 Oleh Petrenko
// Copyright (C) 2022-2024 Oleh Petrenko
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -236,7 +236,7 @@ fun kernelInit(params UefiPayload) {

// GUI
// TODO should be done at main thread
// TODO use some kind of autorun.ini asset
// TODO use some kind of autorun.ini/.bat asset
if true {
for i in 3
{
Expand Down

0 comments on commit f494057

Please sign in to comment.