OSDev.org
https://forum.osdev.org/

About Memory/Task management...
https://forum.osdev.org/viewtopic.php?f=15&t=32557
Page 1 of 1

Author:  interruption [ Sat Nov 11, 2017 3:23 pm ]
Post subject:  About Memory/Task management...

Let's say I have a task A that has a specific block of allocated memory. How this task gets into memory and execution is not of consideration; let's just say it is there and somehow it gets processor time.

Let's say this task A has a memory leak that goes beyond the bounds of its allocation, and that this threatens to overwrite all of memory.

Can anyone list EVERY SINGLE POSSIBLE WAY that you can prevent task A from accessing memory outside of its allocation in a X86 32 bit execution environment?

Note that this is a LIST, if you have an idea you would consider to be risky/horrible/just bad, then I still want to hear it.

Also note that task A has full control of the processor at this moment, unless somehow it could be interrupted at the moment of accessing memory outside of its allocation.

I am sorry if this is a stupid question or a repeat, but I have looked a while on this form and I have not really found any question quite as comprehensive as this.

Author:  nielsd [ Sat Nov 11, 2017 3:45 pm ]
Post subject:  Re: About Memory/Task management...

A process should never be able to get full control of the CPU, if it does, then there's no point in protecting the memory pages.
Your process gets virtual memory assigned by your kernel, so the kernel controls what memory can and cannot be written to/read from. If your process allocates memory and there's no assigned virtual memory left for that process, it should request more virtual memory.

The kernel receives this request (probably using a syscall) and can for example check if the process isn't using too much resources yet.

Author:  alexfru [ Sat Nov 11, 2017 3:47 pm ]
Post subject:  Re: About Memory/Task management...

interruption wrote:
Let's say I have a task A that has a specific block of allocated memory. How this task gets into memory and execution is not of consideration; let's just say it is there and somehow it gets processor time.

Let's say this task A has a memory leak that goes beyond the bounds of its allocation, and that this threatens to overwrite all of memory.

Can anyone list EVERY SINGLE POSSIBLE WAY that you can prevent task A from accessing memory outside of its allocation in a X86 32 bit execution environment?

Note that this is a LIST, if you have an idea you would consider to be risky/horrible/just bad, then I still want to hear it.

Also note that task A has full control of the processor at this moment, unless somehow it could be interrupted at the moment of accessing memory outside of its allocation.

I am sorry if this is a stupid question or a repeat, but I have looked a while on this form and I have not really found any question quite as comprehensive as this.


If you don't control what's in the task, then segmentation, page translation, interpretation/emulation of task's code (hardware-assisted virtualization is just a variation on the theme of memory protection with page translation).
If you do, you can just insert checks into the task.

Author:  interruption [ Sat Nov 11, 2017 5:36 pm ]
Post subject:  Re: About Memory/Task management...

Quote:
A process should never be able to get full control of the CPU, if it does, then there's no point in protecting the memory pages.


Sorry; what I meant really was that the process is currently running on the CPU, and the kernel is not currently running, so the process has control over the CPU unless the CPU prevents it from accessing memory, or the OS can somehow be called through an interrupt when the process accesses out of bounds memory.

Quote:
Your process gets virtual memory assigned by your kernel, so the kernel controls what memory can and cannot be written to/read from. If your process allocates memory and there's no assigned virtual memory left for that process, it should request more virtual memory.

The kernel receives this request (probably using a syscall) and can for example check if the process isn't using too much resources yet.


Isn't that basically describing paging? Actually, the entire purpose of this post was to see if there were any alternatives to paging because the pages take up a fair amount of memory on a system with memory constraints, and I don't really need all of the functionalities it promises. I just need to be able to prevent a task from overwriting kernel space.

Quote:
If you don't control what's in the task, then segmentation, page translation, interpretation/emulation of task's code (hardware-assisted virtualization is just a variation on the theme of memory protection with page translation).


Can you elaborate more on each system and roughly give like how much memory it uses and such? I have seen some of what you are saying and have an idea of it, but what I was asking more for was like a bulleted list of each process and perhaps a brief overview on what it entails. I know that it is somewhat vague and perhaps hard/tedious to map out.

Quote:
If you do, you can just insert checks into the task.


Lets just go with the initial premise that assuming an arbitrary task that can contain anything is in memory, and it is running at the moment.
I just want to prevent it from overwriting memory outside of its allocated space; if the task causes other problems such as processor/operating system exceptions, than I can deal with that. I just want it to stay in it's bounds.

Author:  zaval [ Sat Nov 11, 2017 5:50 pm ]
Post subject:  Re: About Memory/Task management...

the next/previous page after/before the last/first page in this block allocated for the process has an entry in the page table indicating its not present status, causing processor to generate a page fault exception on access to it. it's a basics of virtual memory protection. there are also attributes for preventing only writes, execute.

Author:  interruption [ Sat Nov 11, 2017 6:03 pm ]
Post subject:  Re: About Memory/Task management...

So, in other words, paging is the only way that you can control an arbitrary process and make sure it obeys the bounds of memory?

Author:  ~ [ Sat Nov 11, 2017 6:16 pm ]
Post subject:  Re: About Memory/Task management...

I posted here a full 32-bit page table/directory for the 4GB address space:
viewtopic.php?t=32513

I think that this sample paging table might clear the initial confusion associated on how to use paging since it displays how to build it more clearly and shows the whole range of identity-mapped memory.


You could start your kernel by making it contain static paging tables that are identity mapped. You could create paging tables dynamically for each new process from there. You could use several schemes, for example reserve some lower memory to map the kernel globally in other processes as read-only, indirect write or privileged write, or you could have a fully dynamic address space, but locating and reallocating things scattered around for the kernel and system modules would be too hard for a start.

In fact you could start a kernel with the most difficult structures (PCI address space, page tables, I/O maps, etc.) as static tables that you could later free dynamically and replace/modify as needed.

Author:  FallenAvatar [ Sat Nov 11, 2017 11:08 pm ]
Post subject:  Re: About Memory/Task management...

This is either a homework question, in which case say so, or you are just trying to figure out what to use/research, in which case use paging. And ignore ~, he has a habit of spewing misinformation.

- Amy

Author:  Brendan [ Sun Nov 12, 2017 3:19 am ]
Post subject:  Re: About Memory/Task management...

Hi,

interruption wrote:
Isn't that basically describing paging? Actually, the entire purpose of this post was to see if there were any alternatives to paging because the pages take up a fair amount of memory on a system with memory constraints, and I don't really need all of the functionalities it promises. I just need to be able to prevent a task from overwriting kernel space.


This is very wrong.

Paging costs a tiny bit of memory (about 0.2%) for page tables, etc; which is relatively insignificant. However, it also allows you to do various tricks (allocation on demand, copy on write, memory mapped files, etc) that can save you a lot of memory; and it allows you to do other tricks (swap space, etc) that allow you to use more memory than you actually have.

If a computer has 4 GiB of RAM and is running processes that all use 512 MiB each; an OS that doesn't use paging probably won't be able to handle more than 7 processes; an OS that uses paging will probably handle 15 processes without using swap space; and with 40 GiB of swap space a process that uses paging will probably handle 30 processes before the user notices any performance difference (and will probably handle 150 processes before it becomes too slow to be usable).

interruption wrote:
So, in other words, paging is the only way that you can control an arbitrary process and make sure it obeys the bounds of memory?


For 80x86 protected mode (and not long mode which doesn't support segmentation); there's paging, segmentation and software protection. These aren't mutually exclusive, so (ignoring "no protection") there's 7 possible permutations (paging only, segmentation only, software only, paging+segmentation, paging+software, segmentation+software, paging+segmentation+software).

Note: "software" is some combination of special language/tools and/or special run-time (e.g. JIT interpreter).

"Paging only" gives the best compromise between complexity, performance, protection and usefulness; so almost nobody uses the other possibilities. Note: for debugging (where you don't care about complexity, performance or protection) "software" can be extremely powerful, and because of this there are some tools (e.g. valgrind) that are capable of providing "paging+software" on top of an OS that is intended as "paging only".


Cheers,

Brendan

Author:  alexfru [ Sun Nov 12, 2017 3:26 am ]
Post subject:  Re: About Memory/Task management...

interruption wrote:
Quote:
If you don't control what's in the task, then segmentation, page translation, interpretation/emulation of task's code (hardware-assisted virtualization is just a variation on the theme of memory protection with page translation).


Can you elaborate more on each system and roughly give like how much memory it uses and such? I have seen some of what you are saying and have an idea of it, but what I was asking more for was like a bulleted list of each process and perhaps a brief overview on what it entails. I know that it is somewhat vague and perhaps hard/tedious to map out.


See Intel® 64 and IA-32 Architectures Software Developer’s Manual.
Specifically, volume 3, System Programming Guide:
Chapter 2 — System Architecture Overview
Chapter 3 — Protected-Mode Memory Management
Chapter 4 — Paging
Chapter 5 — Protection

That's as elaborate as it gets. I don't want to restate it. You'll still need to take a deep dive to make use of this functionality, whether for protection you're asking about or for memory management in general. For yet another overview (if you find chapter 2 insufficient), look up our wiki, Wikipedia or just google stuff up.

Author:  azblue [ Mon Nov 13, 2017 8:17 pm ]
Post subject:  Re: About Memory/Task management...

interruption wrote:
Isn't that basically describing paging? Actually, the entire purpose of this post was to see if there were any alternatives to paging because the pages take up a fair amount of memory on a system with memory constraints...


I think you're thinking paging requires just over 4MB of RAM; it does not. Assuming 4KB page sizes and PAE disabled, CR3 points to one 4K table, and each of the 1024 entries there point to another 4K table, each of which point to the actual page. If you're using <= 4MB of contiguous, aligned virtual memory, your "upper" table (pointed to by CR3) will have 1 present entry and 1023 not present entries, and your "lower" page table (pointed to by the one present entry in the "upper" table) will point to the actual physical RAM you're using. Thus, you need as little as 8KB for your page tables, and only an additional 4KB for every additional contiguous aligned 4MB of virtual memory you need.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/