Jump to content

  • Log In with Google      Sign In   
  • Create Account

OS and application interaction questions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Tispe   Members   -  Reputation: 1048

Like
0Likes
Like

Posted 25 May 2012 - 07:24 AM

Let me bring you down to the very core of a CPU, we have the ALU, CU, Registers, CLK, and PC. Outside of the CPU we have memory and in between the memory controller. For the sake of simplicity lets not dwell on cache and "unimportant" CPU sub-systems like pipelining.

On start-up the operating system loads the application binary (EXE file) to memory and it sets the Program Counter (PC) to the Address of the first instruction of the binary block. After execution of the first instruction the PC is incremented and the next instruction is fetched, decoded and executed. This goes on until some interrupt happens or the application has ended.

Before the application was started by the OS, the "context" (registers) was saved on stacks, and after the application finished the context is retrieved.


Now to the point; multitasking. The Scheduler(software) is supposed to allow multiple applications to share the same core by using different schemes.

But how does the Scheduler actually interrupt the application when it has full control?
Is there a timer that interrupts the CPU every 10ms unless it is reset, triggering some Scheduler code?
Is it this we call a preemptive scheduler?
Does the OS before launching a new thread set the CPU to operate in "user mode" limiting some instructions?
What is the difference in processes and services from this perspective?
How does the application allocate memory without hazard, through the memory controller?
How can the OS restrict the application to its own address space, user mode, can't the application disable "user mode" itself?


Also, where can I find the x86 ISA standard? I am interested in assembly language for x86, any compendium / video lessons out there?

Sponsor:

#2 bradbobak   Members   -  Reputation: 1154

Like
1Likes
Like

Posted 25 May 2012 - 09:07 AM

But how does the Scheduler actually interrupt the application when it has full control?
Is there a timer that interrupts the CPU every 10ms unless it is reset, triggering some Scheduler code?


On x86, you can set up a timer interrupt to trigger every so often.

Does the OS before launching a new thread set the CPU to operate in "user mode" limiting some instructions?


On x86, most os's will set the 'ring level' to 3, limiting some cpu commands (like loading the page table, 'hlt' ing the cpu, etc., making some io ports unavailable)


How does the application allocate memory without hazard, through the memory controller?


Generally, it sends a 'syscall' irq, where when the os recieves it, it maps some memory for the application, possibly adding a page table entry.

How can the OS restrict the application to its own address space, user mode, can't the application disable "user mode" itself?


On x86, the 'kernel' sets the process to ring level 3, limiting many commands. Restricting the address space is basically done by the kernel setting up a unique page table for the process itself before handing over control to the process.

www.osdev.net may have documentation that will help you out.

#3 demonkoryu   Members   -  Reputation: 976

Like
0Likes
Like

Posted 25 May 2012 - 10:05 AM

Also, http://www.osdever.net/tutorials/index

#4 Firestryke31   Members   -  Reputation: 350

Like
0Likes
Like

Posted 25 May 2012 - 01:34 PM

http://wiki.osdev.org/Main_Page

Note that all of what follows depends on the OS you're running, but in a general high level overview, this is how it works.

Basically, the OS keeps a copy of the CPU's registers for every thread. Every so often it interrupts a thread and saves all of it's CPU state. It then loads the CPU state from the next scheduled thread, and continues that thread (part of the loaded/saved state is the PC). If the thread is in a different task, the scheduler will probably load the new task's memory context.

Each process has it's own page directory (memory context), dividing up the physical RAM and placing it around the process's address space. The only way one process can see memory from another process is if the OS puts the page of RAM (typically 4KB) into both address spaces. The OS then protects the page directory and tables by saying only ring 0 can write to it.

x86/x64 has 4 'rings' of protection, though most only use ring 0 (OS) and ring 3 (userspace). Switches between the rings are tightly controled by the OS. Ring switches are almost always either interrupts (hardware notifying the CPU it needs attention now) or system calls (the app requesting a service, like memory allocation or talking to another process)

The typical x86/x64 platform has several timers that can be set to interrupt the CPU at any frequency requested (among other hardware that will also interrupt the CPU when requested, like disk IO finishing or PS/2 data waiting). The OS tells the CPU what to do for which interrupts in a table of basically function pointers. The OS then protects the interrupt tables by saying only ring 0 can write to it. Whenever the CPU recieves a timer interrupt, it jumps to the piece of code that the OS tells it to, which usually executes the sceduler.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS