Пример #1
0
        /// <summary>
        /// allocate memory. The memory is set to zero.
        /// </summary>
        public static Addr AllocateCleared(USize n, GFP flags)
        {
            var addr = Allocate(n, flags);

            MemoryOperation.Clear(addr, n);
            return(addr);
        }
Пример #2
0
        /// <summary>
        /// allocate memory for an array. The memory is set to zero.
        /// </summary>
        public static Addr AllocateArrayCleared(USize elements, USize size, GFP flags)
        {
            var total = elements * size;
            var addr  = Allocate(total, flags);

            MemoryOperation.Clear(addr, total);
            return(addr);
        }
Пример #3
0
    //============================================

    private long ApplyMask1(MemoryOperation op)
    {
        var m1 = Convert.ToInt64(op.mask.Replace("X", "0"), 2);
        var m0 = Convert.ToInt64(op.mask.Replace("X", "1"), 2);

        var val = (op.value | m1) & m0;

        return(val);
    }
Пример #4
0
        private void SetupBasicStructure(Addr entriesAddr)
        {
            KernelMessage.WriteLine("Setup PageTable");
            MemoryOperation.Clear4(entriesAddr, InitalMemoryAllocationSize);

            PageDirectoryEntries = (PageDirectoryEntry *)entriesAddr;
            PageTableEntries     = (PageTableEntry *)(entriesAddr + InitalPageDirectorySize);

            // Setup Page Directory
            PageDirectoryEntry *pde = PageDirectoryEntries;
            PageTableEntry *    pte = PageTableEntries;

            KernelMessage.WriteLine("Total Page Entries: {0}", InitialPageTableEntries);
            KernelMessage.WriteLine("Total Page Dictionary Entries: {0}", InitialDirectoryEntries);

            //for (int pidx = 0; pidx < InitialPageTableEntries; pidx++)
            //{
            //    pte[pidx] = new PageTableEntry
            //    {
            //        Present = true,
            //        Writable = true,
            //        User = true,
            //        PhysicalAddress = (uint)(pidx * 4096),
            //    };
            //}

            for (int didx = 0; didx < InitialDirectoryEntries; didx++)
            {
                pde[didx] = new PageDirectoryEntry
                {
                    Present        = true,
                    Writable       = true,
                    User           = true,
                    PageTableEntry = &pte[didx * PagesPerDictionaryEntry],
                };
            }

            // Unmap the first page for null pointer exceptions
            //MapVirtualAddressToPhysical(0x0, 0x0, false);

            PrintAddress();
        }
Пример #5
0
    private IEnumerable <MemoryOperation> GetAllOperations(MemoryOperation op)
    {
        var val = Convert.ToString(op.address, 2)
                  .PadLeft(op.mask.Length, '0')
                  .ToCharArray();

        for (var i = 0; i < op.mask.Length; i++)
        {
            if (op.mask[i] is '1' or 'X')
            {
                val[i] = op.mask[i];
            }
        }

        foreach (var addr in GenerateAddresses(new string(val)))
        {
            yield return op with {
                       address = addr
            }
        }
        ;
    }
Пример #6
0
        private void ManualSetID()
        {
            m = new MemoryOperation(pId);
            string text = textBox2.Text;

            try
            {
                if (m != null)
                {
                    int    value = (int)baseAdres + 26949482;
                    byte[] bytes = Encoding.ASCII.GetBytes(text);
                    m.WriteProcessMemory((IntPtr)value, bytes);
                }
            }
            catch (Exception ex)
            {
                Exception ex3;

                Invoke(new Action(delegate()
                {
                    MessageBox.Show(this, ex.Message + "\r\n" + ex.StackTrace, "ERROR");
                }));
            }
        }
Пример #7
0
        public bool Ready(MemoryOperation op)
        {
            if (_memoryOperationActive)
            {
                switch (op)
                {
                case MemoryOperation.LoadAddress:
                    // Can't start a new Load operation until the current one is finished.
                    return(false);

                case MemoryOperation.Read:
                    // Read operations take place on cycles 5 and 6
                    return(_memoryCycle > 4);

                case MemoryOperation.Store:
                    if (_systemType == SystemType.AltoI)
                    {
                        // Store operations take place on cycles 5 and 6
                        return(_memoryCycle > 4);
                    }
                    else
                    {
                        // Store operations take place on cycles 3 and 4
                        return(_memoryCycle > 2);
                    }

                default:
                    throw new InvalidOperationException(String.Format("Unexpected memory operation {0}", op));
                }
            }
            else
            {
                // Nothing running right now, we're ready for anything.
                return(true);
            }
        }
Пример #8
0
 public static void InitializeGCMemory()
 {
     // Wipe GCMemory from Boot loader
     MemoryOperation.Clear4(Address.GCInitialMemory, Address.GCInitialMemorySize);
 }
Пример #9
0
 public static void Clear(this MemoryRegion region)
 {
     MemoryOperation.Clear(region.Start, region.Size);
 }
Пример #10
0
 public Memory()
 {
     locations       = new Int16[65536];
     memoryOperation = MemoryOperation.READ;
 }
Пример #11
0
            public unsafe void ShiftUp()
            {
                var sizePerChar = (uint)sizeof(ConsoleChar);

                MemoryOperation.Copy4((uint)Chars + ((uint)Columns * sizePerChar), (uint)Chars, ((uint)Rows - 1) * (uint)Columns * sizePerChar);
            }
Пример #12
0
        private static unsafe Process CreateProcessFromElf(ElfSections elf, string path, uint argumentBufferSize = 0)
        {
            var proc = CreateEmptyProcess(new ProcessCreateOptions()
            {
                User = true
            });

            KernelMessage.WriteLine("Create proc: {0}, PID: {1}", path, proc.ProcessID);
            proc.Path      = path;
            proc.PageTable = PageTable.CreateInstance();

            var allocator = new UserInitialPageAllocator()
            {
                DebugName = "UserInitial"
            };

            allocator.Setup(new MemoryRegion(500 * 1024 * 1024, 60 * 1024 * 1014), AddressSpaceKind.Virtual);
            proc.UserPageAllocator = allocator;

            // Setup User PageTable
            proc.PageTableAllocAddr = VirtualPageManager.AllocatePages(
                KMath.DivCeil(proc.PageTable.InitalMemoryAllocationSize, 4096),
                new AllocatePageOptions {
                Pool = PageAllocationPool.Identity
            });
            PageTable.KernelTable.SetWritable(proc.PageTableAllocAddr, proc.PageTable.InitalMemoryAllocationSize);
            proc.PageTable.UserProcSetup(proc.PageTableAllocAddr);
            proc.PageTable.Map(proc.PageTableAllocAddr, proc.PageTableAllocAddr, proc.PageTable.InitalMemoryAllocationSize);

            proc.PageTable.MapCopy(PageTable.KernelTable, BootInfoMemoryType.KernelTextSegment);
            proc.PageTable.SetExecutable(BootInfoMemoryType.KernelTextSegment);
            proc.PageTable.MapCopy(PageTable.KernelTable, Address.InterruptControlBlock, 4096);
            proc.PageTable.MapCopy(PageTable.KernelTable, KernelMemoryMapManager.Header->Used.GetMap(BootInfoMemoryType.GDT));
            proc.PageTable.MapCopy(PageTable.KernelTable, KernelMemoryMapManager.Header->Used.GetMap(BootInfoMemoryType.IDT));
            proc.PageTable.MapCopy(PageTable.KernelTable, KernelMemoryMapManager.Header->Used.GetMap(BootInfoMemoryType.TSS));

            var tmpKernelElfHeaders = SetupElfHeader(proc, elf);

            // Setup ELF Sections
            for (uint i = 0; i < elf.ProgramHeaderCount; i++)
            {
                var section = elf.GetProgramHeader(i);

                var memSize  = section->MemSz;
                var fileSize = section->FileSz;
                var virtAddr = section->VAddr;
                var srcAddr  = elf.GetProgramPhysAddr(section);

                if (memSize == 0)
                {
                    continue;
                }

                KernelMessage.WriteLine("Setup Program Section VAddr {0:X8} SrcAddr {1:X8} Size {2:X8}", virtAddr, srcAddr, memSize);

                if (virtAddr == Addr.Zero)
                {
                    var mem = allocator.AllocatePagesAddr(KMath.DivCeil(memSize, 4096));
                    tmpKernelElfHeaders[i].Addr = mem;
                    virtAddr = mem;
                }

                // Map the Sections
                proc.PageTable.MapCopy(PageTable.KernelTable, srcAddr, virtAddr, memSize);
                if (i == 0) // TODO: Flags
                {
                    proc.PageTable.SetReadonly(virtAddr, memSize);
                }

                if (memSize > fileSize)
                {
                    MemoryOperation.Clear(srcAddr + fileSize, memSize - fileSize);
                }

                //if (name->Equals(".text"))
                //    proc.PageTable.SetExecutable(virtAddr, size);
            }
            KernelMessage.WriteLine("proc sections are ready");

            for (uint i = 0; i < elf.SectionHeaderCount; i++)
            {
                var section = elf.GetSectionHeader(i);

                var size     = section->Size;
                var virtAddr = section->Addr;
                var srcAddr  = elf.GetSectionPhysAddr(section);

                if (size == 0)
                {
                    continue;
                }

                var name = elf.GetSectionName(section);
                if (virtAddr == Addr.Zero)
                {
                }
                else
                {
                    if (name->Equals(".bss"))
                    {
                        MemoryOperation.Clear(srcAddr, size);
                        proc.BrkBase = virtAddr + size;
                        KernelMessage.WriteLine("sbrk_base: {0:X8}", proc.BrkBase);
                    }
                }
            }

            // Detect Thread-Main
            var entryPoint = GetMainEntryPointFromElf(elf);

            KernelMessage.WriteLine("EntryPoint: {0:X8}", entryPoint);
            var defaultDispatchEntryPoint = GetDispatchEntryPointFromElf(elf);

            if (defaultDispatchEntryPoint != Addr.Zero)
            {
                KernelMessage.WriteLine("DispatchEntryPoint: {0:X8}", defaultDispatchEntryPoint);
                proc.Service.Init(defaultDispatchEntryPoint);
            }

            var mainThread = Scheduler.CreateThread(proc, new ThreadStartOptions(entryPoint)
            {
                ArgumentBufferSize  = argumentBufferSize,
                AllowUserModeIOPort = true,
                DebugName           = "UserProcMainThread",
            });

            KernelMessage.WriteLine("Created Process {0} ProcessID={1}", path, proc.ProcessID);

            return(proc);
        }
Пример #13
0
 private static unsafe void SetupKernelSection()
 {
     // TODO: Respect section program header address.
     // Currently, we can make a raw copy of ELF file
     MemoryOperation.Copy4(Address.OriginalKernelElfSection, Address.KernelElfSectionPhys, OriginalKernelElf.TotalFileSize);
 }
Пример #14
0
        private void SetupBasicStructure(Addr entriesAddr)
        {
            KernelMessage.WriteLine("Setup PageTable");
            SetAddress(entriesAddr);

            MemoryOperation.Clear4(AddrPageDirectoryPT, InitalMemoryAllocationSize);

            // uint mask = 0x00004000;
            // uint v1 = 0x00000007;
            // uint r1 = v1.SetBits(12, 52, mask, 12);

            // ulong v2 = v1;
            // ulong r2 = v2.SetBits(12, 52, mask, 12);
            // uint r2Int = (uint)r2;

            // KernelMessage.WriteLine("r1: {0:X8}", r1);
            // KernelMessage.WriteLine("r2: {0:X8}", r2Int);

            // Setup Page Directory
            PageDirectoryPointerTableEntry *pdpt = (PageDirectoryPointerTableEntry *)AddrPageDirectoryPT;
            PageDirectoryEntry *            pde  = (PageDirectoryEntry *)AddrPageDirectory;
            PageTableEntry *pte = PageTableEntries;

            KernelMessage.WriteLine("Total PDPT: {0}", InitialDirectoryPTEntries);
            KernelMessage.WriteLine("Total Page Dictionary Entries: {0}", InitialDirectoryEntries);
            KernelMessage.WriteLine("Total Page Table Entries: {0}", InitialPageTableEntries);

            //for (int pidx = 0; pidx < InitialPageTableEntries; pidx++)
            //{
            //    pte[pidx] = new PageTableEntry
            //    {
            //        Present = true,
            //        Writable = true,
            //        User = true,
            //        PhysicalAddress = (uint)(pidx * 4096),
            //    };
            //}

            for (int didx = 0; didx < InitialDirectoryEntries; didx++)
            {
                pde[didx] = new PageDirectoryEntry
                {
                    Present        = true,
                    Writable       = true,
                    User           = true,
                    PageTableEntry = &pte[didx * PagesPerDictionaryEntry],
                };
            }

            for (int ptidx = 0; ptidx < InitialDirectoryPTEntries; ptidx++)
            {
                pdpt[ptidx] = new PageDirectoryPointerTableEntry
                {
                    Present            = true,
                    PageDirectoryEntry = &pde[ptidx * PagesPerDictionaryEntry],
                };
            }

            // Unmap the first page for null pointer exceptions
            //MapVirtualAddressToPhysical(0x0, 0x0, false);

            PrintAddress();
        }