About Memory/Task management...

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
interruption
Posts: 22
Joined: Wed Feb 08, 2017 7:51 pm

About Memory/Task management...

Post by interruption »

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.
User avatar
nielsd
Member
Member
Posts: 31
Joined: Sun Apr 05, 2015 3:15 pm

Re: About Memory/Task management...

Post by nielsd »

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.
osdev project, goal is to run wasm as userspace: https://github.com/kwast-os/kwast
alexfru
Member
Member
Posts: 1111
Joined: Tue Mar 04, 2014 5:27 am

Re: About Memory/Task management...

Post by alexfru »

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.
interruption
Posts: 22
Joined: Wed Feb 08, 2017 7:51 pm

Re: About Memory/Task management...

Post by interruption »

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.
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.
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.
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.
User avatar
zaval
Member
Member
Posts: 656
Joined: Fri Feb 17, 2017 4:01 pm
Location: Ukraine, Bachmut
Contact:

Re: About Memory/Task management...

Post by zaval »

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.
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).
interruption
Posts: 22
Joined: Wed Feb 08, 2017 7:51 pm

Re: About Memory/Task management...

Post by interruption »

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?
User avatar
~
Member
Member
Posts: 1227
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Re: About Memory/Task management...

Post by ~ »

I posted here a full 32-bit page table/directory for the 4GB address space:
http://f.osdev.org/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.
YouTube:
http://youtube.com/@AltComp126

My x86 emulator/kernel project and software tools/documentation:
http://master.dl.sourceforge.net/projec ... ip?viasf=1
FallenAvatar
Member
Member
Posts: 283
Joined: Mon Jan 03, 2011 6:58 pm

Re: About Memory/Task management...

Post by FallenAvatar »

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
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: About Memory/Task management...

Post by Brendan »

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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
alexfru
Member
Member
Posts: 1111
Joined: Tue Mar 04, 2014 5:27 am

Re: About Memory/Task management...

Post by alexfru »

interruption wrote:
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.
azblue
Member
Member
Posts: 147
Joined: Sat Feb 27, 2010 8:55 pm

Re: About Memory/Task management...

Post by azblue »

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.
Post Reply