Skip to content

Commit

Permalink
Merge branch 'master' into feature/plugNet
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaProductions authored Oct 26, 2023
2 parents 36122ec + 16d769b commit 9a24f18
Show file tree
Hide file tree
Showing 86 changed files with 79,894 additions and 154 deletions.
129 changes: 99 additions & 30 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ name: Build and Test

on:
workflow_dispatch:
# pull_request:
# branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

build:

runs-on: windows-2022
runs-on: self-hosted

outputs:
testkernels: ${{ steps.testkernels_output.outputs.testkernels }}

env:
USER_KIT_PATH: 'C:\CosmosRun\'
BOCHS_RUN_CMD: '\"C:\Program Files (x86)\Bochs-2.6.8\Bochs.exe\" -q -f \"%1\"'
BochsPath: 'C:\Program Files (x86)\Bochs-2.6.8\Bochs.exe'

steps:
- name: Checkout Cosmos
Expand All @@ -22,37 +25,27 @@ jobs:
repository: CosmosOS/Cosmos
path: Cosmos

# - name: Checkout Common
# uses: actions/checkout@v3
# with:
# repository: CosmosOS/Common
# path: Common
- name: Checkout Common
uses: actions/checkout@v3
with:
repository: CosmosOS/Common
path: Common

- name: Checkout IL2CPU
uses: actions/checkout@v3
with:
repository: CosmosOS/IL2CPU
path: IL2CPU

# - name: Checkout XSharp
# uses: actions/checkout@v3
# with:
# repository: CosmosOS/XSharp
# path: XSharp
- name: Checkout XSharp
uses: actions/checkout@v3
with:
repository: CosmosOS/XSharp
path: XSharp

- name: Setup MSBuild
uses: microsoft/[email protected]

- name: Setup Registry
run: |
Write-Host "Setting Cosmos User Kit Path to $($env:USER_KIT_PATH)"
reg add HKLM\SOFTWARE\WOW6432Node\Cosmos /v UserKit /d "$($env:USER_KIT_PATH)"
reg query HKLM\SOFTWARE\WOW6432Node\Cosmos
Write-Host "Setting Bochs Run Command to $($env:BOCHS_RUN_CMD)"
reg add HKCR\BochsConfigFile\shell\Run\command /ve /d "$($env:BOCHS_RUN_CMD)"
reg query HKCR\BochsConfigFile\shell\Run\command
- name: Nuget - Cache
uses: actions/cache@v3
with:
Expand All @@ -71,26 +64,102 @@ jobs:
- name: Build - IL2CPU
run: |
dotnet publish IL2CPU\source\IL2CPU -r win7-x86 -o "$($env:USER_KIT_PATH)Build\IL2CPU\"
dotnet publish IL2CPU\source\IL2CPU -o "$($env:USER_KIT_PATH)Build\IL2CPU\"
- name: Copy Output - Cosmos
run: |
xcopy /Y "Cosmos\source\Cosmos.Build.Tasks\tools\grub2\boot\grub\i386-pc\*.*" "$($env:USER_KIT_PATH)Build\ISO\boot\grub\i386-pc\"
xcopy /Y "Cosmos\Build\grub2\boot\grub\grub.cfg" "$($env:USER_KIT_PATH)Build\ISO\boot\grub"
xcopy /Y /S "Cosmos\Build\VMware\*" "$($env:USER_KIT_PATH)Build\VMware\"
xcopy /Y /S "Cosmos\Build\Tools" "$($env:USER_KIT_PATH)Build\Tools\"
xcopy /Y /S "Cosmos\Resources\Bochs\*.*" "%ProgramFiles(x86)%\Bochs-2.6.8\"
- name: Copy Output - IL2CPU
run: |
xcopy /Y "IL2CPU\source\Cosmos.Core.DebugStub\*.xs" "$($env:USER_KIT_PATH)XSharp\DebugStub\"
- name: Test - Cosmos
# - name: Send test kernels to GITHUB_OUTPUT
# id: testkernels_output
# run: |
# $testList = dotnet test Cosmos\Tests\Cosmos.TestRunner.UnitTest\Cosmos.TestRunner.UnitTest.csproj --list-tests
# Write-Host $testList
# $testList >> tests.txt
# $match = Select-String -Pattern "^.*TestKernel\((.*)\)$" -Path tests.txt
# Write-Host $match
# $testKernels = $match.matches.groups | where { $_.index % 2 -ne 0 } | Select-Object value | foreach { $_.value }
# Write-Host $testKernels
# $testKernelsEnv = ""
# $testKernels | foreach { $testKernelsEnv += "$($_)," }
# $testKernelsEnv = $testKernelsEnv.Trim(",")
# Write-Host $testKernelsEnv
# "testkernels=[$($testKernelsEnv)]" >> $env:GITHUB_OUTPUT

test:
if: ${{ success() }}
needs: [build]
runs-on: self-hosted

strategy:
matrix:
kernel: [
"BoxingTests.Kernel",
"Cosmos.Compiler.Tests.TypeSystem.Kernel",
"Cosmos.Compiler.Tests.Bcl.Kernel",
"Cosmos.Compiler.Tests.Bcl.System.Kernel",
"Cosmos.Compiler.Tests.Exceptions.Kernel",
"Cosmos.Compiler.Tests.MethodTests.Kernel",
"Cosmos.Compiler.Tests.SingleEchoTest.Kernel",
"Cosmos.Kernel.Tests.Fat.Kernel",
"Cosmos.Kernel.Tests.IO.Kernel",
"SimpleStructsAndArraysTest.Kernel",
"Cosmos.Kernel.Tests.DiskManager.Kernel",
"GraphicTest.Kernel",
"NetworkTest.Kernel",
"AudioTests.Kernel",
"MemoryOperationsTest.Kernel",
"ProcessorTests.Kernel"
]

env:
USER_KIT_PATH: 'C:\CosmosRun\'
BochsPath: 'C:\Program Files (x86)\Bochs-2.6.8\Bochs.exe'

steps:
- name: Checkout Cosmos
uses: actions/checkout@v3
with:
repository: CosmosOS/Cosmos
path: Cosmos

- name: Checkout Common
uses: actions/checkout@v3
with:
repository: CosmosOS/Common
path: Common

- name: Checkout IL2CPU
uses: actions/checkout@v3
with:
repository: CosmosOS/IL2CPU
path: IL2CPU

- name: Checkout XSharp
uses: actions/checkout@v3
with:
repository: CosmosOS/XSharp
path: XSharp

- name: Copy Bochs
run: |
dotnet test Cosmos\Tests\Cosmos.TestRunner.UnitTest\Cosmos.TestRunner.UnitTest.csproj --logger "trx;LogFileName=$($env:USER_KIT_PATH)TestResult.xml"
xcopy /Y /S "Cosmos\Resources\Bochs\*.*" "${env:ProgramFiles(x86)}\Bochs-2.6.8\"
- name: Test - Cosmos
env:
CI: "True"
run: |
dotnet test "Cosmos\Tests\Cosmos.TestRunner.UnitTest\Cosmos.TestRunner.UnitTest.csproj" --logger "trx;LogFileName=$($env:USER_KIT_PATH)TestResults\${{ matrix.kernel }}-TestResult.trx" --filter "FullyQualifiedName~${{ matrix.kernel }}"
- name: Upload Test Logs
uses: actions/upload-artifact@v3
with:
name: test-logs
path: $($env:USER_KIT_PATH)TestResult.xml
path: ${{ env.USER_KIT_PATH }}TestResults
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Docs/log.txt
*.docstates
*.vsp
*.pdb
*.lock.json
Thumbs.db
build.force

Expand Down
14 changes: 8 additions & 6 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<Project>

<PropertyGroup>
<LangVersion>Latest</LangVersion>
<NoWarn>CA1051;CA1501;CA1707;CA1711;CA1801;CS1572;CS1573;CS1574;CS1591;$(NoWarn)</NoWarn>
<SignAssembly>True</SignAssembly>
</PropertyGroup>

<PropertyGroup>
<RepoRoot>$(MSBuildThisFileDirectory)</RepoRoot>
<CommonRepoRoot>$(RepoRoot)..\Common\</CommonRepoRoot>
<IL2CPURepoRoot>$(RepoRoot)..\IL2CPU\</IL2CPURepoRoot>
<XSharpRepoRoot>$(RepoRoot)..\XSharp\</XSharpRepoRoot>
</PropertyGroup>

<PropertyGroup>
<LangVersion>Latest</LangVersion>
<NoWarn>CA1051;CA1501;CA1707;CA1711;CA1801;CS1572;CS1573;CS1574;CS1591;NU1603;$(NoWarn)</NoWarn>
<SignAssembly>True</SignAssembly>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
</PropertyGroup>

<PropertyGroup>
<!--
This property sets the suffix for local builds.
Expand Down
19 changes: 18 additions & 1 deletion Docs/articles/Kernel/MemoryManagement.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ The RAT is managed through the `RAT` class. Pages are allocated via `void* RAT.A

The Heap itself is managed by the `Heap` class. It contains the mechanism to allocate (`byte* Heap.Alloc(uint aSize)`), re-allocate ('byte* Heap.Realloc(byte* aPtr, uint newSize)') and free (`void Heap.Free(void* aPtr)`) objects of various sizes. Objects are seperated by size in bytes into Small (Smaller than 1/4 Page), Medium (Smaller than 1 Page) and Large (Larger than 1 Page). Currently Medium and Large objects are managed the same way using the methods in `HeapLarge` which do little more than allocating/freeing the necessary number of pages. Small objects are managed differently in `HeapSmall`.

Small Objects are managed using the SMT (Size Map Table), which is initalised using `void HeapSmall.InitSMT(uint aMaxItemSize)`. The basic idea of the SMT is to allocate objects of similar sizes on the same page. The SMT grows dynamically as required. The SMT is made up of a series of pages, each of which contains a series of `RootSMTBlock` each of which link to a chain of `SMTBlock`. The `RootSMTBlock` can be thought of as column headers and the `SMTBlock` as the elements stored in the column. The `RootSMTBlock` are a linked list, each containing the maximum object size stored in its pages, the location of the first `SMTBlock` for this size, and the location of the next `RootSMTBlock`. The list is in ascending order of size, so that the smallest large enough `RootSMTBlock` is found first. A `SMTBlock` contains a pointer to the actual page where objects are stored, how much space is left on that page, and a pointer to the next `SMTBlock`. If every `SMTBlock` for a certain size is full, a new `SMTBlock` is allocated. The page linked to by the `SMTBlock` is split into an array of spaces, each large enough to allocate an object of maximum size with header, which can be iterated through via index and fixed size when allocating. Each object allocated on the `HeapSmall` has a header of 2 `ushort`, the first one storing the actual size of the object and the second, the GC status of the object.
Small Objects are managed using the SMT (Size Map Table), which is initalised using `void HeapSmall.InitSMT(uint aMaxItemSize)`.
The basic idea of the SMT is to allocate objects of similar sizes on the same page. The SMT grows dynamically as required.
The SMT is made up of a series of pages, each of which contains a series of `RootSMTBlock` each of which link to a chain of `SMTBlock`.
The `RootSMTBlock` can be thought of as column headers and the `SMTBlock` as the elements stored in the column.
The `RootSMTBlock` are a linked list, each containing the maximum object size stored in its pages, the location of the first `SMTBlock` for this size, and the location of the next `RootSMTBlock`.
The list is in ascending order of size, so that the smallest large enough `RootSMTBlock` is found first.
A `SMTBlock` contains a pointer to the actual page where objects are stored, how much space is left on that page, and a pointer to the next `SMTBlock`.
If every `SMTBlock` for a certain size is full, a new `SMTBlock` is allocated.
The page linked to by the `SMTBlock` is split into an array of spaces, each large enough to allocate an object of maximum size with header, which can be iterated through via index and fixed size when allocating.
Each object allocated on the `HeapSmall` has a header of 2 `ushort`, the first one storing the actual size of the object and the second, the GC status of the object.

## Garbage Collection

Expand All @@ -45,6 +54,14 @@ The garbage collector has to be manually triggerd using the call `int Heap.Colle

Note that the GC does not track objects only pointed to by pointers. To ensure that the GC nevertheless does not incorrectly free objects, you can use `void GCImplementation.IncRootCount(ushort* aPtr)` to manually increase the references of your object by 1. Once you no longer need the object you can use `void GCImplementation.DecRootCount(ushort* aPtr)` to remove the manual reference, which allows the next `Heap.Collect` call to free the object.

`Heap.Collect` only cleans up the objects which are no longer used but will leave behind empty pages in the SMT.
These pages can be cleaned up using `HeapSmall.PruneSMT` which will return the number of pages it freed.
Note that if in future elements are reallocated, this will cause new pages in the SMT to be allocated again, so using this too often may not be useful.

## Automatically Trigger Garbage Collection

When `RAT.MinFreePages` is set to a positive value and the number of free pages (as tracked by `RAT.FreePageCount`) drops below this value, on page allocation `Heap.Collect` will automatically be called. Each time this happens the value in `RAT.GCTriggered` is incremented by one.

### Internals

The garbage collector uses the tracing approach, which means that during collection a graph of all reachable objects is created and all non-discovered objects are freed. The garbage collector will only check objects on pages which have a type where the `GCManaged` bit is set. The graph is created by starting from "root" objects which are either stored in static variables or part of the current stack. Each of these objects is "marked" and all objects referenced by this object are recursivly also "marked" and "swept". This is done using the methods `void Heap.MarkAndSweepObject(void* aPtr)` for objects and `void Heap.SweepTypedObject(uint* obj, uint type)` for structures. For this to work each allocated object holds a 1bit flag if the object was discovered during the marking phase and a 7bit value counter for the number of static references it has. The number of static references an object has is updated using `void GCImplementation.IncRootCount(ushort* aPtr)` and similar methods, which are called from the Stsfld opcode.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,20 @@ The following is a non-exhaustive list of features that Cosmos offers:
- A basic audio interface

> **Note**
> Use [embeded resources](https://cosmosos.github.io/articles/Kernel/ManifestResouceStream.html) instead of the VFS for now for assets.
> Use [embeded resources](https://cosmosos.github.io/articles/Kernel/ManifestResouceStream.html) instead of the FS for now.
## Setting it up

<hr/>
Cosmos has an article [here](https://cosmosos.github.io/install.html) on how to do that.

## Documentation

For instructions on how to install and use Cosmos, please visit the [Cosmos website](http://www.gocosmos.org).
For documentation and technical information, see the [Cosmos Documentation](https://cosmosos.github.io).
The Cosmos documentation can be found [here](https://cosmosos.github.io/api/Cosmos.Build.Common.html).

If you have questions about how to use Cosmos, want to show off what you have made or have general questions, go check out [GitHub Discussions](https://github.com/CosmosOS/Cosmos/discussions). Alternatively, you can also join our [Discord server](https://discord.com/invite/kwtBwv6jhD)! If you think you found a bug in Cosmos, please check existing [issues](https://github.com/CosmosOS/Cosmos/issues) first before opening a new one.
If you still have any questions on how to use Cosmos, you can open a [discussion](https://github.com/CosmosOS/Cosmos/discussions) or you can join the [Discord server](https://discord.com/invite/kwtBwv6jhD)!

[Devkit Change Log](https://cosmosos.github.io/articles/Changelog.html)
The devkit changelog can be found [here](https://cosmosos.github.io/articles/Changelog.html).

</p>
## Reporting an issue

If you think you found a bug in Cosmos, please check existing [issues](https://github.com/CosmosOS/Cosmos/issues) first before opening a new one. Do **not** open an issue if you need help with something in Cosmos that is not a bug, if you don't know how to code it's not a Cosmos issue for example.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": 1,
"dependencies": {
".NETFramework,Version=v4.8": {
"Dapper.StrongName": {
"type": "Direct",
"requested": "[2.0.90, )",
"resolved": "2.0.90",
"contentHash": "oYmC8zT97TBlmNcU/xM54CJC6sgG+VUmUhRN5tg05P1Y3H4wx7vU+xBM6MgEJ21aXAXsTctchPs5TCsbRdIxJw=="
},
"Microsoft.CSharp": {
"type": "Direct",
"requested": "[4.7.0, )",
"resolved": "4.7.0",
"contentHash": "pTj+D3uJWyN3My70i2Hqo+OXixq3Os2D1nJ2x92FFo6sk8fYS1m1WLNTs0Dc1uPaViH0YvEEwvzddQ7y4rhXmA=="
}
}
}
}
3 changes: 0 additions & 3 deletions Test.sln
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cosmos.Core.Memory.Test", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cosmos.Core_Asm", "source\Cosmos.Core_Asm\Cosmos.Core_Asm.csproj", "{B7077A34-D7F0-4422-BE7C-65DF26C65489}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TheRingMaster", "source\TheRingMaster\TheRingMaster.csproj", "{3DD192AF-2D72-449F-936C-ED8734225B18}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Spruce", "..\XSharp\source\Spruce\Spruce.csproj", "{FF46829E-B612-4D36-80BE-ED04521AD91A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cosmos.Compiler.Tests.TypeSystem", "Tests\Kernels\Cosmos.Compiler.Tests.TypeSystem\Cosmos.Compiler.Tests.TypeSystem.csproj", "{D21A7C6C-A696-4EC3-84EB-70700C1E3B34}"
Expand Down Expand Up @@ -867,7 +865,6 @@ Global
{FB23BD72-AEC3-485E-B86C-8E7DB0B3BB9B} = {29EEC029-6A2B-478A-B6E5-D63A91388ABA}
{408E5ACC-EA9A-41E8-AA95-514C5F47BD34} = {52D81759-C7CC-427F-8C96-89CA10C914B5}
{B7077A34-D7F0-4422-BE7C-65DF26C65489} = {04B18FFC-8EA0-4E9F-9E1B-478527B19AFA}
{3DD192AF-2D72-449F-936C-ED8734225B18} = {C286932C-3F6D-47F0-BEEF-26843D1BB11B}
{FF46829E-B612-4D36-80BE-ED04521AD91A} = {E9CD521E-C386-466D-B5F7-A5EB19A61625}
{D21A7C6C-A696-4EC3-84EB-70700C1E3B34} = {ECEA7778-E786-4317-90B9-A2D4427CB91C}
{0DF97CAC-220B-4DAD-B397-42E394255763} = {ECEA7778-E786-4317-90B9-A2D4427CB91C}
Expand Down
Loading

0 comments on commit 9a24f18

Please sign in to comment.