views:

246

answers:

8

How does one programmatically cause the OS to switch off, go away and stop doing anything at all so that a program may have complete control of a PC system?

I'm interested in doing this from both an MS Windows and Linux environments. Any languages or APIs considered.

I want the OS to stop preempting my program, stop its virtual memory management, stop its device drivers and interrupt service routines from running and basically just go away. Then, when my program has had its evil way with the bare metal, I want the OS to come back again without a reboot.

Is this even possible?

A: 

Not as such, no.

What you want is basically an application that becomes an OS; a severely stripped down Linux kernel coupled with some highly customized and minimized tools might be the way to go for this.

Williham Totland
Not impossible; it's been done, as a variant of hibernation. See my post. :)
bdonlan
Indeed. I stand corrected.
Williham Totland
+13  A: 

With Linux, you could use kexec jump to transfer control completely to another kernel (ie, your program). Of course, with great power comes great responsibility - it is entirely up to you to service interrupts, and avoid corrupting the old kernel's memory. You'll end up having to write your own OS kernel to do this. Also, the transfer of control takes quite some time, as the kernel has to de-initialize all hardware, then reinitialize it when it's time to resume. Since kexec jump was originally designed for hibernation support, this isn't a problem in its original context, but depending on what you're doing, it might be a problem.

You may want to consider instead working within the framework given to you by the OS - just write a normal driver for whatever you're doing.

Finally, one more option would be using the linux Real-Time patchset. This lets you assign static priorities to everything, even interrupt handlers; by running a process with higher priority than anything else, you could suspend /nearly/ everything - the system will still service a small stub for interrupts, as well as certain interrupts that can't be deferred, like timing interrupts, but for the most part the heavy work will be deferred until you relinquish control of the CPU.

Note that the RT patchset won't stop virtual memory and the like - mlockall will prevent page faults on valid pages though, if that's enough for you.

Also, keep in mind that whatever you do, the system BIOS can still cause SMM traps, which cannot be disabled, except by motherboard-model-specific methods.

bdonlan
+1  A: 

Not really. Operating Systems are a foundation, and your program runs on top of them. The OS handles memory access, disk writing operations, communications, etc. when your application makes requests, and asking the OS to move out of the way would mean that your program would have to do the OS's job instead.

taserian
A: 

Yeah dude, you can totally do that, you can also write a program to tell my bank to give you all my money and send you a hot Russian.

luvPlsQL
what's not funny about this people!
luvPlsQL
You unwittingly happened upon my very idea, damn you!
Paul Bullough
+1: I'll show ya' some love! :) -2 seemed harsh for an obvious joke. :)
Greg D
+2  A: 

There are lots of really ugly ways to do this. You could modify the running kernel by writing some trampoline code to /dev/kmem that passes control to your application. But I wouldn't recommend attempting something like that!

Basically, you would need to have your application act as its own operating system. If you want to read data from a file, you would have to figure out where the data lives on disk, and generate your own SCSI requests to talk to the disk drive. You would have to implement your own interrupt handler to get notified when the data is ready. Likewise you would have to handle page faults, memory allocation, etc. Most users feel that this isn't worth the effort...

Why do you want to do this?

Is there something that your application needs to do that the OS won't let it do? Are you concerned with the OS impact on performance? Something else?

Keith Smith
There are legitimate reasons to want to do something like this. For instance, to get dependable hard-realtime response from the system. See my answer below.
T.E.D.
I agree that if you want real-time response, you will want to get Windows (or Linux) "out of the way." But I would still argue that the sane way to achieve that is to run a real-time operating system (or executive) between the hardware are the application, rather than having the application completely take over the HW. If that's what the OP is after, I agree that the RTX approach is a good one. (FWIW, years ago I worked for VenturCom, whose technology is now part of IntervalZero's RTX...)
Keith Smith
A: 

if you were devious, and wanted to avoid alot of the operating system housekeeping you could probably hook yourself into a driver routine. Thinking out aloud, verging on hacking. google how to write root kits.

Phill
+1  A: 

If you don't mind shelling out some cash, you could use IntervalZero's RTX to do this for a Windows system. It's a hard realtime subsystem that gets installed on a Windows box as sort of a hack into the HAL and takes over the machine, letting Windows have whatever CPU cycles are left over.

It has its own scheduler and device drivers, but if you run your program at the top RTX priority, don't install any RTX device drivers (or disable interrupts for the duration), then nothing will interrupt it.

It also supports a small amount of interaction with programs on the Windows side.

We use it as a nice way to get a hard realtime box that runs Windows.

T.E.D.
+1  A: 
ephemient