每周源代码15-小型托管操作系统版

Thanks to Thijs Kroesbergen for the pointer to this week's source. He turned me on to a tiny Operating System written in C# called "Cosmos (C# Open Source Managed Operating System)". As the project says, why? Because it's fun! I wrote a Tiny Virtual OS in C# for an Operating Systems class I took while I was going to school at night.

感谢Thijs Kroesbergen提供了指向本周资料的指针。 他让我转向了一个用C#编写的名为“ Cosmos(C#开源托管操作系统) ”的小型操作系统。 如项目所述,为什么? 因为好玩! 我为晚上上学时上的操作系统课用C#编写了Tiny Virtual OS

And so, Dear Reader, I present to you fifteenth in a infinite number of posts of "The Weekly Source Code." Here's some source I was reading this week.

因此,尊敬的读者,我每周源代码的无数帖子中向您展示了第十五名。 这是我本周正在阅读的一些资料。

First, I went back and looked at some of my source code from the Tiny OS project. It was written, largely at night, in .NET 1.1. It was for a 10 week (one term) project, and I wrote it all in a weekend so as to have the rest of the term free.

首先,我回去看了一些从我的源代码微小的OS项目。 它主要是在晚上用.NET 1.1编写的。 这是一个为期10周(一个学期)的项目,我在周末将所有内容都写了下来,以使其余学期免费。

It's pretty horrific to read old source. I would propose that if you can look at old code you wrote and feel good about it that you're either insane, deranged or a really good programmer. One of those three.

阅读旧资料真是太恐怖了。 我建议,如果您可以看一下您编写的旧代码,并对此感到满意,那么您要么是疯子,要么是精神错乱的,要么是一个真正优秀的程序员。 这三个之一。

Do remember that this project was a tiny Virtual OS. We were given an Instruction Set for our little processor and a format for programs and the goal was to write an OS to run them.

请记住,该项目是一个很小的虚拟OS。 我们为我们的小型处理器提供了一套指令集,并为程序提供了一种格式,目的是编写一个操作系统来运行它们。

Here's the constructor for my MemoryManager. I'm sure doing a lot of work in the constructor! One of the fun little things I did for this Tiny OS was to swap memory pages as XML Files. It was a little joke because XML was such overkill and overhead for something so nuanced as a memory manager. I figured since I was simulating an OS's behavior using something as high-level as .NET, then why shouldn't I swap fake memory to disk as XML?

这是我的MemoryManager的构造函数。 我确定在构造函数中要做很多工作! 我为此Tiny OS做的一件有趣的小事情是将内存页面交换为XML文件。 这是个小玩笑,因为XML对于像内存管理器这样细微的东西来说是太过分了,而且开销很大。 自从我使用诸如.NET的高级内容来模拟操作系统的行为以来,我就知道了,为什么我不应该将伪内存作为XML交换到磁盘上呢?

I wanted the source for this Tiny Virtual OS to ready like all the psuedo-code we'd been looking at in the Operating Systems books.

我希望这个Tiny Virtual OS的源代码能够像我们在《操作系统》一书中看到的所有伪代码一样准备好。

Looking back, I think it'd have been cool if I'd made a WinForms host and did a graphical view of memory that would allow folks to see things like memory fragmentation. Maybe I'll do one in WPF or as an XBAP, it'd be a cool learning tool for some of the students I talk to.

回顾过去,我认为如果我制作WinForms主机并以图形方式查看内存,这将使人们看到内存碎片之类的东西,那真是太酷了。 也许我会使用WPF或XBAP来做一个,对于某些与我交谈的学生来说,这将是一个很酷的学习工具。

public MemoryManager(uint virtualMemSizeIn)
{
	// Find a size for addressableMemory that is on a page boundary
	virtualMemSize = CPU.UtilRoundToBoundary(virtualMemSizeIn, CPU.pageSize);

	// Size of memory must be a factor of CPU.pageSize
	// This was asserted when the CPU initialized memory
	uint physicalpages = (uint)(CPU.physicalMemory.Length/CPU.pageSize);
	uint addressablepages = (uint)(virtualMemSize/CPU.pageSize);
	
	_pageTable = new ArrayList((int)addressablepages);

	// Delete all our Swap Files
	foreach (string f in Directory.GetFiles(".","*.xml"))
		File.Delete(f);

	// For all off addressable memory...
	// Make the pages in physical and the pages that aren't in physical
	for (uint i = 0;i < virtualMemSize; i+=CPU.pageSize)
	{
		// Mark the Pages that are in physical memory as "false" or "not free"
		MemoryPage p;
		if (i < CPU.physicalMemory.Length) 
		{
			p = new MemoryPage(i, true);
			freePhysicalPages[(int)(i/CPU.pageSize)] = false;
		}
		else p = new MemoryPage(i, false);

		_pageTable.Add(p);
	}
}

Now my OS a trinket, to be clear. Cosmos, on the other hands, is darned interesting. How could you create a REAL OS (meaning an actual bootable OS off of hardware, be it virtual or physical, using IL? You translate the IL to ASM, of course. Very cool and darned clever.

现在,我的操作系统成了一个小饰品。 另一方面,波斯菊很有趣。 您如何使用IL创建一个REAL OS(即从硬件(虚拟的还是物理的)开始的实际可启动OS,无论是虚拟的还是物理的)?您当然可以将IL转换为ASM。非常酷而且聪明。

Cosmos includes a compiler (IL2CPU, which is part of Cosmos) that reads the input file (usually the shell) and Cosmos libraries and compiles the resulting IL to x86 code. IL2CPU has a layer for cross platform and we plan to support other processors and platforms, including x64. IL2CPU also supports certain extension methods which allow C# code to interact directly with the CPU, registers, and ports in the kernel. IL2CPU contains some inline assembler, but there are no ASM files that need to be linked in.

Cosmos包括一个编译器(IL2CPU,它是Cosmos的一部分),它读取输入文件(通常是外壳)和Cosmos库,并将生成的IL编译为x86代码。 IL2CPU具有用于跨平台的层,我们计划支持其他处理器和平台,包括x64。 IL2CPU还支持某些扩展方法,这些方法允许C#代码直接与内核中的CPU,寄存器和端口进行交互。 IL2CPU包含一些内联汇编器,但是没有需要链接的ASM文件。

Currently IL2CPU first outputs raw asm files (with IL comments) and then processes them through nasm (a free assembler). Later we plan to emit directly to binary.

当前,IL2CPU首先输出原始的asm文件(带有IL注释),然后通过nasm(免费的汇编程序)对其进行处理。 稍后,我们计划直接将其发出为二进制。

The scenarios that Cosmos could be used in are very interesting. Because it's easy to write to and easy to build, you could create little mini-OSes with just the features you want. You could make an OS that just does DNS, or just does some REST service. Who knows. (Yes, I know you could also do a stripped down Linux). There is also talk about getting Cosmos to work on the Wii.

可以使用Cosmos的场景非常有趣。 因为它易于编写和构建,所以您可以仅创建具有所需功能的微型操作系统。 您可以制作一个仅执行DNS或仅执行某些REST服务的操作系统。 谁知道。 (是的,我知道您也可以精简Linux)。 也有关于让Cosmos在Wii上工作的讨论。

The example below is from Indy.IL2CPU.Assembler.X86.Native. As you can see, IL2CPU writes out ASM.

下面的示例来自Indy.IL2CPU.Assembler.X86.Native 。 如您所见,IL2CPU会写出ASM。

protected override void EmitDataSectionHeader(string aGroup, StreamWriter aOutputWriter) {
    base.EmitDataSectionHeader(aGroup, aOutputWriter);
    if (aGroup == MainGroup) {
          aOutputWriter.WriteLine("section .data");
          aOutputWriter.WriteLine("_start:  ");
          aOutputWriter.WriteLine("; multiboot header ");
          aOutputWriter.WriteLine("MBFLAGS equ 0x03 ; 4KB aligned modules etc., full memory info,  ");
          aOutputWriter.WriteLine("                        ; use special header (see below) ");
          aOutputWriter.WriteLine("dd 0x1BADB002           ; multiboot signature ");
          aOutputWriter.WriteLine("dd MBFLAGS              ; 4kb page aligment for modules, supply memory info ");
          aOutputWriter.WriteLine("dd -0x1BADB002-MBFLAGS  ; checksum=-(FLAGS+0x1BADB002) ");
          aOutputWriter.WriteLine("; other data - that is the additional (optional) header which helps to load  ");
          aOutputWriter.WriteLine("; the kernel. ");
          aOutputWriter.WriteLine("; end of header ");
          aOutputWriter.WriteLine("MultiBootInfo_Memory_High dd 0");
          aOutputWriter.WriteLine("MultiBootInfo_Memory_Low dd 0");
 }

There's lots of methods like this that do the hard work. The orchestration, however, is up in Engine.cs, where assemblies are taken appear via reflection, and their methods are taken apart using the obvious (my psuedo-code):

有很多类似的方法可以完成艰苦的工作。 但是,编排在Engine.cs中进行,其中通过反射显示装配体,并使用显而易见的(我的伪代码)将其方法分开:

foreach Type in Assembly    foreach Method in Type       ProcessMethod into ASM (via ProcessAllMethods)

程序集中的foreach类型将ProcessMethod类型转换为ASM中的foreach方法(通过ProcessAllMethods)

Using an interesting ILReader, a useful class in and of itself. Here's a trimmed chunk from ProcessAllMethods that uses the ILReader.

使用有趣的ILReader本身就是一个有用的类。 这是使用ILReader的ProcessAllMethods的一部分。

At this point, we've loaded an assembly, got a type, and we're sitting on xCurrentMethod. They take the method, check for some exception handling (I've trimmed that, it's tedious) and they get an OpCode, and then using the current Assembler, they assemble that operation. Lather, rinse, repeat as necessary.

至此,我们已经加载了程序集,获得了类型,并且我们正坐在xCurrentMethod上。 他们采用了该方法,检查了一些异常处理(我对此进行了修剪,这很乏味),并且他们获得了OpCode,然后使用当前的Assembler对其进行组装。 泡沫,冲洗,必要时重复。

ILReader xReader = new ILReader(xCurrentMethod);
    while (xReader.Read()) {
    if (mInstructionsToSkip > 0) {
        mInstructionsToSkip--;
        continue;
    }
    ExceptionHandlingClause xCurrentHandler = null;
    ...snip...        
    xMethodInfo.CurrentHandler = xCurrentHandler;
    xOp = GetOpFromType(mMap.GetOpForOpCode(xReader.OpCode), xReader, xMethodInfo);
    if ((!xOp.SupportsMetalMode) && mAssembler.InMetalMode) {
      throw new Exception("OpCode '" + xReader.OpCode + "' not supported in Metal mode!");
    }
    xOp.Assembler = mAssembler;
    new Comment("StackItems = " + 
      mAssembler.StackContents.Count + ", 
      Top item = " + 
       (mAssembler.StackContents.Count > 0 ? mAssembler.StackContents.Peek().ToString() : "(empty)"));
       xOp.Assemble();
    }

There's not a lot of comments in the Cosmos Project, but once you get your head around what they are doing, it's a pretty amazing piece of work, and I can see why they are having so much fun.

Cosmos项目中没有很多评论,但是一旦您了解了他们的工作,这是一件非常了不起的工作,我可以理解为什么他们会这么开心。

From their site at http://www.gocosmos.org:

从他们的网站http://www.gocosmos.org

If you just want to play with Cosmos:

如果您只想和Cosmos一起玩:

  1. Install the user kit.

    安装用户工具包

  2. Join the discussion list

    加入讨论列表

Other resources:

其他资源:

  1. Read the FAQ.

    阅读常见问题解答

  2. Subscribe to the Cosmos Blog

    订阅Cosmos博客

  3. Documentation - Most developer documentation right now.

    文档-现在大多数开发人员文档。

If you are interested in kernel development: 如果您对内核开发感兴趣:
  1. Get source from CodePlex

    CodePlex获取源

  2. Read the full requirements. They are pretty basic though, and everything you need except for Windows (For development) is free.

    阅读全部要求 它们是非常基本的,除了Windows(用于开发)以外,您需要的所有东西都是免费的。

  3. Read Getting Started

    阅读入门

Enjoy!

请享用!

翻译自: https://www.hanselman.com/blog/the-weekly-source-code-15-tiny-managed-operating-system-edition

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个小型操作系统,采用gcc进行开发,几千行的代码,方便初学者学习,内含有编译好的映像文件,及bochs模拟器配置文件,可在bochs下模拟运行。如下为源码包内的README文件: # # Snixos Project version 1.0, 2003.6 # (C) Copyright 2003,2004,2005 Jockeyson,KeqianGao # All Rights Reserved. # Distributed under the terms of the GNU General Public License. # # This program is a free and open source software and you can redistribute # it and/or modify it under the terms of the GNU General Public License as # published by the Free Software Foundation. As no any liability is assumed # for any incidental or consequential damages in connection with the # information or program fragments contained herein,so any exception arised # is at your own risk. It is ABSOLUTELY WITHOUT ANY WARRANTY. # Bug report please send to Snallie@tom.com . # 0. What is Snixos Project? Snixos Project is an experimental operating system designed by Jockeyson , KeqianGao aiming at multitask/multithread, Chinese envionment including Chinese characters input and ouput etc... 1. What you need to run Snixos PC with the following minimum configuration: CPU: i386 or above MEM: 2M extended memory or more DISPLAY: VGA or above Floppy Drive: 1.44M 2. Compile Environment RH Linux 7.2 or above (kernel 2.4.7-10 ) NASM 0.98 gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-98) 3. Compile and install a. unpack the source package tar zxvf snixos_x.x_YYYYMMDD_HHMMSS.tgz b. cd to snixos cd snixos c. before make insert a 1.44M floppy in drive c.1 to make snixos run in VGA text mode make clean make VIDEO=TEXT diskimg make floppy c.2 to make snixos run VGA graphics mode make clean make diskimg make floppy d. bootup with the floppy just created in step c also you can follow this way to run snixos in bochs either in Windows or Linux a. just as the above b. just as the above c. invoke these command: make clean; make diskimg you'll get a snixos ima
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值