Virtual addressing in os - operating-system

If programme loaded into the ram at the phsycal spaces and the cpu reads addresses from ram so what is the purpose of the virtual address spacing. At the end cpu will read everything from ram's phsycal addrssses. I mean does mmu converts ram's phsycal address into virtual address and sends it to the cpu? To be more clear as i know cpu reads programme from ram. But it seems the programme's virtual addresses loaded to cpu somehow and then cpu tries to find the loaded address from ram. Is this right?
Thanks in advance.

Related

How does Cpu generate the logical address for the disk

If you think question is not proper please edit or make it correct, i am asking what i google and extract from the internet.
Cpu generates the logical address which is converted into physical address but the question here is how does the cpu generates the logical address for the data that is stored on the disk.
Cpu generates the logical address which is converted into physical address but the question here is how does the cpu generates the logical address for the data that is stored on the disk.
It doesn't, at least not the way you're thinking it does.
Normally a program tries to access memory at a virtual address, but the CPU sees "virtual address isn't present" and complains to the OS (kernel) via. a page fault. The page fault handler figures out what went wrong, loads the data from disk into RAM, maps the RAM into the virtual address space, then lets the program continue/retry as if nothing happened. The second time the CPU tries to execute the code the data is in RAM so it works fine.
Of course the OS has to know the reason why data at a virtual address wasn't present, which means that the OS has to keep track of extra information that the CPU doesn't have - if the virtual address actually isn't valid at all (e.g. NULL), or if the data is in swap space (and where), or if the data is part of a memory mapped file (and which offset of which file).
There is Virtual address space and a physical address space. virtual address space defines the address space of the program. say there is a program of 4GB. in that case we can represent the address space for that program as 32 bits. (2^32 = 4GB) from 0 to 0xFFFFFFFF.
this is the space the program thinks it has.
while compilation of the program, the program is given logical addresses based on the address space of the program.
after loading in to the memory. the program counter that is assign to this program will point to these addresses (logical/virtual addresses) and cpu will only want to fetch these addresses where the program instruction's are. cpu doesn't know where are the instructions located in the memory. that is up to MMU to translate the addresses.
the main thing is CPU doesn't actually generate these addresses, these are the addresses that where given to the program while compilation, using these, the instructions in the program reference each other. so cpu just see what program counter is pointing and generate / asks for these instruction. which are located in the physical memory.
when ever a address for fetching data or operand , instruction pointed by the PC, cpu call for these addresses.

How are the sizes of pointers determined in computer systems? Via virtual or physical addresses?

I have an exam tomorrow on virtual memory address translation and I'm rather confused on this topic. I know the CPU will generate a virtual address to then access a physical address. So if we have a system with 32 bit virtual addresses, and 64 bit physical addresses, then the pointers for user level processes I'm guessing will be 8 bytes.
My logic is because the virtual address is being translated to the physical address, so this number will always be coming from the physical address.
No, user-space processes work only with virtual addresses (32-bit in your example).
The memory they "see" is their own private virtual address space. (They can make system calls like mmap and munmap to request that pages in that address-space be backed by files, or by anonymous RAM like for C malloc.) But they don't know anything about where in physical memory those pages are located.
The OS can even "fake it" by paging out some of their pages to swap space / page file, and handling the page fault if the process touches such a page by doing I/O to bring it back in and then waking up the process to rerun the load or store instruction that page faulted.
Hardware translates virtual addresses to physical addresses on every memory access. To make this fast, a TLB caches recently-used translations. On a TLB miss, hardware does a "page walk", reading the page tables to find the right virtual page->physical page translation.
The OS manages the page tables, choosing any physical page as "backing" for a virtual page.
Physical addresses wider than virtual?
Under a multi-tasking OS, multiple processes can be running. Each one has its own 32-bit (4GiB) virtual address space.
The size of physical address space limits how much RAM you can put in a machine total, and can be different from how much any single process can use at once. Changing page tables is faster than reading from disk, so even if it can't all be mapped at once, a kernel can still make use of lots of physical RAM for pagecache (cache of file contents from disk).
More importantly, multiple processes can be running, each with their own up-to-4GiB of virtual address space backed by physical memory, up to the amount of physical RAM in the system. On a CPU with multiple cores, these can be running simultaneously, truly allowing simultaneous use of more than 4GB of RAM. But not by any single process.
x86 is a good example here: Running an x86-64 kernel with 32-bit user-space gives us pretty much the situation you describe. (A 64-bit kernel can use 64-bit virtual addresses, but nevermind that, just look at user-space.)
You can have several processes each using about 4GiB of physical RAM.
The x86-64 page-table format has room for physical addresses as wide as 52-bit, although current HW doesn't use that many. (Only as wide as the amount of RAM it actually supports attaching. Saves bits in the TLBs, and other parts of the CPU). https://en.wikipedia.org/wiki/X86-64#Architectural_features
Before x86-64, 32-bit x86 kernels could use the same page-table format but with 36-bit physical addresses, on CPUs from Pentium Pro and later.
https://en.wikipedia.org/wiki/Physical_Address_Extension. That allowed up to 64GB of physical RAM. (A 32-bit kernel would typically reserve 1 or 2GB of virtual address space for itself so each process could really only use up to 3 or 2GB, but it's the same idea. Not a problem for 32-bit user-space under a 64-bit kernel though, so that made a simpler example.)
Virtual addresses are visible to user-level processes. They never should never see the physical address. So if virtual addresses are 32-bit, pointers in user-level processes are also 32-bit, i.e. 4 bytes.
The system/kernel then needs to do the translation somehow. It will know the virtual address and must translate it to the physical address, so it will eventually have a physical pointer, 64-bit = 8 byte. But once again, this address/pointer are for "internal use" only.
In practice though, you will have virtual and physical addresses of the same size, matching the word size of the CPU and its architecture (x86 vs x86_64). A virtual to pyhsicial translation will normally need to happen in a page fault, which happens when a user-level process attempts to access memory that is not loaded. To access it in the first place, it needs to have e.g. dereferenced a pointer pointing to that address, which would be done with a memory access instruction of the particular CPU architecture, which is done with word-sized addresses.
The programmer will only see virtual addresses. The physical address space is opaque to the programmer and the user. Therefore, size of a pointer is dependent on the size of the virtual address. In the particular case you have given, the maximum amount of memory your system can consume is dictated by your virtual address space. This is why 32-bit OS on 64-bit hardware is limited to a maximum of 4 gigs of memory. But, in the case of a 64-bit virtual address, even if we have insufficient RAM, we can offload some of the pages to the secondary storage to give the illusion that we have more RAM available. In the case, the page is located in the secondary memory, a page fault occurs and the page is transferred to RAM.
Edit : As Peter said in the comments, the virtual address limit affects the maximum memory a Process can consume.

Where are Logical addresses located?

I know that user program generates logical addresses.Suppose there is a small code snippet in C .When address is printed,the addresses are virtual addresses.My question is where are those addresses fetched from?where exactly do the allocated values and variables stay?At main memory or secondary memory?If main memory then why there is physical address?
User mode programs only see logical addresses. Only the operating system (kernel mode) sees physical memory.
My question is where are those addresses fetched from?
Those are the logical addresses assigned by the program loader ad linker.
where exactly do the allocated values and variables stay?At main memory or secondary memory?
In a virtual memory system, it may be in main memory or secondary storage.
If main memory then why there is physical address?
It is a logical address that is mapped to a physical address using page tables.
I am kind of new to the computer architecture and Operating System,
but I will try to answer as much as I can. As far as I have
understood about the logical address (Which I still have trouble
understanding, about where it is fetched from or where it is stored.
I mean these addresses (numbers) gotta be stored somewhere,
otherwise CPU can't generate it by itself, right?), these addresses
are assigned by CPU or a processor and depends on the CPU
architecture. Each process is assigned a virtual/logical
address. And this logical address is translated to physical address
by Memory Management Unit of CPU (MMU).
Where exactly do the allocated values and variables stay? As user3344003 said, it may be in main memory or secondary storage.
If main memory then why there is physical address? The reason lies
in the concept of Virtual Memory. Each process has its own virtual
address and a page table. Process's logical address are mapped
through this page table to the Physical memory (RAM). Whatever that
logical address is, it gets mapped to Physical address. If the
Physical memory gets full, then OS evicts some of the less used
or unused process to Secondary storage and puts the needed process
in the RAM. That way multiple process can run at the same time.
Every process assumes that they have all the space in RAM just for
themselves. If not for virtual memory, then physical memory would be
full and process might crash and may shut down the OS as well.
Hope it helps. I am still learning, if my understanding about Logical address and virtual memory is wrong then Please comment.

Will OS provide virtual address space?

This is an interview question. I am writing all the details the interviewer gave me. The question is:-
A system has a memory of 1GB. A process requires only 1MB of memory. Assuming no other processes are running in memory, will the OS provide virtual address translation for this process or give the entire 1MB of physical space ?
Assuming this is a virtual memory system and at the risk of gross oversimplification:
Such system would use address translation for any processes, even if only one is running.
Such a system is unlikely to provide the 1MB of memory until the process accesses that much memory. That is, the system will only allocate physical pages on demand.

Virtual Memory Space

What does the virtual memory space size depend on? Does it depend on the RAM or on the architecture or something else.
Basically it depends on the architecture (32bit 64bit and so...).
This is a very simplistic explanation of things, but so called "architecture" limits size of the virtual address space. For example, 32bit architecture will enable to address 2^31 memory addresses.
The size of the RAM will limit the amount of physical memory that can be used, but not the virtual address space. (potentially the Hard-drive can be used to extend the available physical memory)
Anyway I recommend to read the wiki page on virtual memory
Very simply, virtual memory is just a way of letting your software use more memory addresses than there is actual physical memory, such that when the data being access isn't already hosted in physical memory it's transparently read in from disk, and when some more physical memory is needed to do things like that some of the current content of physical memory is temporarily written or "swapped" out to disk (e.g. the least-recently used memory). In other words, some of the physical memory becomes a kind of cache for a larger virtual memory space including hard disk.