How can a program compiled to machine language run on different machines? - operating-system

In school we've been taught that compilers compile a computer program to machine language. We've also been taught that the machine language consists of direct instructions to the hardware. Then how can the same compiled program run on several computer configurations with different hardware?

Depends what you mean by 'different hardware' if it is the same processor (or same family eg Intel x86) then the machine code instructions are the same.
If the extra hardware is different peripherals (screens, disks printers etc) then the operating system hides those details by giving you a consistent set of instructions to drive them
If you mean, how can you run a program for an ARM cpu on an Intel x86, then you can't - except by some sort of virtual machine emulator that reads each of the ARM instructions and either translates them into x86 or runs the same functionality as a set of x86 funcs and then returns the same answer that the ARM ones would have done.
Edit: I assume you mean PCs with different hw - ie different peripherals but the same processor family?
Talking to hardware doesn't involve specific instructions as such - it's mostly a matter of moving memory to specific locations where the operating system and/or device driver have specifically reserved for data going to that device. In the old days of DOS and BIOS you would then trigger an interupt to call a specific bit of code in the BIOS to act on that data and send it to the HW.

With an emulator or a virtual machine, either of which effectively translates the machine language on the fly.

I think it is more accurate to say that native compilers compile to a specific instruction set of a processor. Since there are families of processors that keep backwards compatibility: 8086 - 80386 - 80486 - 80586 - Dual Core - Quad Core...; then each processor runs the instructions of its ancestors. If you want to port your code across processor architectures, then you need for sure a virtual machine or emulator, like it was mentioned previously.

Related

Trying to run Raspberry-Pi image under QEMU, but VM memory is limited to 256MB

I want to build a time consuming package (mediapipe) on my Raspberry-Pi buster image under QEMU. So far, I've gotten the image to load and run (including with network connectivity); however, I'm limited to 256MB of storage, which just isn't enough to do much - especially build a mediapipe. Can someone explain why Raspbian images running under QEMU seem to be limited to 256MB?
I've seen some posts about people running with 512MB and even one with 1GB, but they don't seem to be very successful. Can anyone explain the reason for the restriction, and a potential fix?
The problem here is that a lot of people claim to be running "raspberry pi emulation in QEMU" when they're actually just running Raspbian userspace on top of a kernel for a different machine emulation. So it's easy to be confused if you look at several different tutorials that are really describing entirely different emulation setups. Look for what machine type they pass QEMU.
The "versatilepb" machine type gets used in a lot of tutorials, especially older ones, because it has been in QEMU a long time and it is possible to get it to work with the 1176 CPU that the classic Raspberry Pi boards used. This specific machine has a 256MB maximum memory size, because the real hardware it's emulating has that restriction (it's imposed by the way the physical memory address space is designed). This machine type will never be able to support more RAM, so if you need more then you should ignore any tutorial or setup that uses it.
More recent versions of QEMU really do emulate the actual raspberry pi hardware; these are the raspi0, raspi1ap, raspi2b, raspi3ap, raspi3b machine types. These will have the same amount of RAM as the real raspi hardware they're emulating (either 512MB or 1GB). The downside of these board models is that some of the device emulation is lacking features -- so older QEMU will often not correctly boot a newer kernel, and sometimes devices you would like to use are not present. Also, because the raspi boards hang their ethernet device off the USB controller, the only way to get ethernet on these QEMU models would also be to use a USB ethernet device, eg with:
-device usb-net,netdev=eth0 -netdev user,id=eth0
This probably needs a recent QEMU version to get a working USB controller.
I don't know if there are any tutorials/recipes for running Raspbian on top of the QEMU "virt" board. If there are, this would probably be the best experience, because the virt board permits lots of memory, PCI devices, virtio devices, and is well maintained.

Can I use VxWorks OS as Normal OS on my PC?

I am New to VxWorks. First of all Can I use VxWorks OS as Normal OS on my PC? I mean Can I run my application software on VxWorks OS?
While VxWorks can run on PC hardware, it is not a general-purpose OS for running independent executables. VxWorks is an RTOS library; you statically link it to your application and the whole runs as a monolithic executable.
It does support a command line interface (intended primarily for development and debug), and from that it is possible to dynamically load and link object files, but these are not independent executables in the sense they are in a GPOS; they essentially become part of the monolithic application.
An RTOS such as OS/9 or QNX would be more suited as these can operate more like a GPOS in the sense of loading and executing independently linked executables.
In any event, application software must be specifically built for these targets.
For versions of VxWorks prior to VxWorks 6, the answer by clifford provides a good explanation of why this is not really possible.
VxWorks 6 introduced Real Time Processes (RTPs). These are independant, user mode applications, running on top of the VxWorks OS. Dependant on how the VxWorks OS has been configured and built, these RTP applications may have access to POSIX libraries, and so you may be able to run POSIX applications (eg linux programs) with little modification.
However, these must still be built for VxWorks, ideally linked against your own VxWorks Source Build.
You cannot, however, just pick up any old application and expect it to run. You are never going to get Word or Excel to run.

Booting Linux on iPhone

I tried searching and searching and can't find a reason why one can't boot Linux from iPhone. I am not asking if there are drivers available for Linux or anything like that, I am just wondering why one can't boot Linux on a standard ARM processor ?
There isn't any such thing as a "standard ARM processor". Every ARM-based SoC is a little bit different, and Apple doesn't publish any information about how their SoCs work. (They aren't even standard Cortex-A designs; the Apple A6 and later all use CPU cores which were customized by Apple.)
Additionally, all of Apple's SoCs contain a bootloader in ROM which verifies a cryptographic signature before running any software from flash memory. This makes it impossible to run an operating system which was not signed by Apple.

Are emulation and hardware-assisted virtualization synonyms?

What is the distinction between emulation and Full Virtualization, also called Hardware-assisted virtualizion (HVM)?
From this source, it is not clear what the relationship is.
Full Virtualization or Hardware-assisted virtualizion (HVM) uses
virtualization extensions from the host CPU to virtualize guests. HVM
requires Intel VT or AMD-V hardware extensions. The Xen Project
software uses Qemu to emulate PC hardware, including BIOS, IDE disk
controller, VGA graphic adapter, USB controller, network adapter etc.
Virtualization hardware extensions are used to boost performance of
the emulation. Fully virtualized guests do not require any kernel
support. This means that Windows operating systems can be used as a
Xen Project HVM guest. Fully virtualized guests are usually slower
than paravirtualized guests, because of the required emulation.
Source: Xen Project Wiki
In the following book these terms are considered synonymous.
At one extreme you have full virtualization, or emulation, in which
the virtual machine is a software simulation of hardware, real or
fictional — as long as there’s a driver, it doesn’t matter much.
Products in this category include VMware and QEMU.
Source: The book of Xen
Following are the excerpts from an article describing the actual difference between emulation and HWM. However, the only distinction I can see is, that virtualization enables to create more than one computing environment.
If emulation takes such a toll, why bother? Because we might want to
do one of the following:
Run an OS on a hardware platform for which it was not designed.
Run an application on a device other than the one it was developed for (e.g., run a Windows program on a Mac).
Read data that was written onto storage media by a device we no longer have or that no longer works.
Source: Russell Kay
Virtual machines offer the following advantages:
They're compatible with all Intel x86 computers.
They're isolated from one another, just as if they were physically separate.
Each is a complete, encapsulated computing environment.
They're essentially independent of the underlying hardware.
They're created using existing hardware.
Source: Russell Kay
There is another article, which only supports my hypothesis.
Emulation, in short, involves making one system imitate another. For
example, if a piece of software runs on system A and not on system B,
we make system B “emulate” the working of system A. The software then
runs on an emulation of system A.
In this same example, virtualization would involve taking system A and
splitting it into two servers, B and C.
So lets consider B=C and we have emulation, dont we?
Please note that virtualization is achieved by emulating the hardware components network adapters, USB, hard disk, CD drives etc in software. Thus emulation actually helps achieving virtualization.
Full virtualization is the technique of virtualization in which the guest OS runs unmodified, that is, the guest is not aware of whether it is running in a virtual machine environment or on a physical machine. Initially binary translation of the guest code was done in order to achieve full virtualization, but it wasn't good from performance perspective.
Para virtualization is a technique which requires modifications in the guest Operating System in order to gain better performance.
Hardware assisted virtualization is full virtualization technique as the guest Operating System runs unmodified. It is called hardware assisted because this type of virtualization utilizes virutalization specific extensions in host hardware like Intel-vtx, AMD-V etc. This technique not only offers full virtualization (guest OS does not require modification) but also has performance benefits and major vendors like Intel and AMD are providing extensions in hardware to support virtualization.

hypervisors and java virtual machine

The questions I would like to ask are:
1) What exactly does hypervisor do? Why is it needed?
2) What is the difference between hypervisor and Java Virtual mMchine?
3) Does JVM use a hypervisor?
4) When a host operating system like linux can handle multiple guest operating system,why use hypervisor?
Would be great help if someone shed light on this
A Hypervisor also known as hardware virtualization are a virtualization layer that allows running one or more native operating system on top of it, as if they run on a physical machine. It is similar to emulation but only runs operating systems that would be able to run without the Hyperviser, which are much faster.
Both are virtualization layers. However Java are optimized for performance and portability. While Java are technicaly an emulator, it are much faster than an hyperviser. This can be achieved because the emulated platform are designed for fast emulation. Java do not run x86 or x86_64/amd64 code, it runs something called Bytecode. The technical term for Bytecode are Intermediate Language (IL). It are compiled to code that are native to your processor when you run it, by the Just In Time compiler (JIT). As the JIT do a compilation process it can make sure that the program follows Java:s security constraints, by simply not generating code that violates such constraints. The Hyperviser enforce security constraints by intercepting so called privileged instructions and by emulating devices such as disk drives. This are done because native x86 or x86_64/amd64 code are very hard for a program to understand, and changing it so that it self-enforce security constraints are next to impossible. Java on the other hand runs Bytecode which are easy for a program to understand and chance so that it self-enforce security rules.
The short answer: An hyperviser are slower than Java but allows you to run a multitude of complete operating systems, and all the software available for them. This while Java are faster, but you can only run Java software on it. If you want to run Windows and Office in your virtual machine, you can't do that in Java.
I think I answered this above but no, it use code inspection and modifies the program so that it self-enforce security rules. This can be done because runnable Java application are in a intermediate state called Bytecode, which are easy for Java to understand, inspect, find code that may violate the rules and modify it in order to obey them. This are a rather complex process that have several advantage over hypervisor. The first advantage are "compile once run everywhere", as Java are compiled and distributed as bytecode. The second advantage are speed, JIT:ed code have the same speed as non-virtualized code even when strict security are enforced. The disadvantage are that only Bytecode programs can run, so you for example cannot run Windows or Linux inside the virtual machine.
If you are running another operating system like Windows or another Linux distribution - you are running an Hyperviser. KVM, Xen and VirtualBox are examples of Hypervisors. You can also run multiple instances of Linux with one shared kernel, known as OS-based virtualization or "Container". But a Container share the kernel and therefor you can only virtual machines with the OS you are running. The advantage with Containers it are more lightweight as you do not need to run multiple kernels on top of each other...
Hypervisor or virtual machine manager, is a program that allows multiple operating systems to share a single hardware host.
JVM or Java Virtual Machine interprets bytecode for a computers processor so that it can perform Java program instructions.
No JVM does not use hypervisor as it is not an virtual machine that runs an OS rather it is just a interpreter.
A host operating system manages different VMs using Hypervisor or virtual machine manager
Before answering your questions, I would recommend you search related entries in wikipedia. A hypervisor is used to run multiple guest OSes while JVM is used to interpret java byte code. JVM runs on top of OS and it doesn't care whether the OS runs on top of bare metal or on a hypervisor. Actually, linux can handle multiple guest operating systems with KVM which is part of the linux kernel. So the description of the last question is totally wrong.