Linux for Macintosh 68K Port

“I don't care if space aliens ate my mouse” or a case study in both the technical and human issues in porting the Linux OS to a new M68K target platform.
Building and Booting Linux

The next stage in the operation was to figure out how to boot a Linux kernel image on the Macintosh. NetBSD and OpenBSD use a boot loader which loads a.out format executables into the memory of the Macintosh, shuts the Macintosh down, moves it to address 0 and jumps to it. I quickly decided I didn't want to write a boot loader. The OpenBSD loader was almost pure MacOS wizardry at a level far beyond my abilities. Not to worry—it soon became apparent that the OpenBSD loader could be persuaded to load Linux too. A true loader could wait.

The next problem was building a Linux kernel image that would link and (while most likely not doing anything useful) at least serve as something to feed the OpenBSD booter. Linux is built using the GNU tool chain, which supports the building of cross compilers. It is thus possible to compile and build 680x0 binaries on an ordinary Intel-based PC. It took a couple of builds to get gcc and the GNU binutils generating almost the right code. Linux a.out executables have a two-byte header different from the OpenBSD ones, and the OpenBSD boot loader checked those bytes. Rather than rebuild the entire tool chain again, I wrote a simple tool to fix the headers.

Most of Linux/M68K was quite content to build for a Macintosh target. I filled in everything that complained with dummy routines—for Mac keyboards, mice, display, etc. until it all compiled. Because of the well-designed abstraction layers in the Linux/M68K kernel, this was quite easy to do. I now had a completely useless, do-nothing, Macintosh kernel that the OpenBSD loader would load and which then promptly crashed the machine as I expected.

The Linux/M68K project had faced up to the challenges of supporting multiple types of 680x0-based computers within the same port, well before I got involved. As a result of the need to support both the Amiga and Atari systems, clear layers of abstractions are present. Adding an additional M68K target consists mostly of filling in platform-specific blank fields. A port to a completely new processor would have been far more challenging than this one.

For the Macintosh case, I filled in various, mostly blank function handlers. After finally getting the thing to link, I ended up with a kernel that was hard-coded for a 5MB 68020-based Macintosh with FPU and a display at 0xF9000000. It had no interrupt controllers, no disk controllers, no keyboard and no mouse. Anything else I could find was also hard coded. However, it linked and that was the important thing. Having done a bit of reading up on the innards of the console drivers (and much interrogation of Jes), I wrote a fairly simplistic back end for the generic console driver on the Macintosh. As it turned out, the very simplistic approach reflected the Macintosh hardware I had, which was a completely unaccelerated bitmapped display supporting 640x480 in 4-bit colour.

Paint It Black

A Linux 68K kernel starts with a partially shared piece of initialisation code written in 680x0 assembler and using almost all the most Gothic and peculiar features of the architecture. This initialisation code also sets up the memory management and caching, and touches everything no one normally knows about. The 68020, 68851, 68881 combination of chips used in the Macintosh II is obsolete and Motorola didn't carry documentation on this device. I did know two things which, in theory, were enough to debug and figure out what was going on. First, I knew the base address of the screen memory; second, I knew the address that the code would begin executing. The very first routine I put in the startup code painted the screen a revolting blue colour. After about 15 boots and some staring at the source code, I had a Macintosh that booted to a blue screen, waited a short while, then crashed.

In many ways, this was the single hardest item to get going. When dealing with a completely unknown system environment with no idea what is around the code, debugging is extremely tricky. Real commercial hardware people use logic analysers—I didn't have that option. I learned several things in the process; notably, that Macintosh screen memory is not located where the hardware claims it is until the MMU is set up. I also made the amazing discovery that the rounded corners on the Macintosh display are drawn in software.

Over the next few weeks, the Macintosh went through an assortment of debugging stripes and coloured patterns as I inched a few lines at a time through the initialisation assembler code, fixing it bit by bit and gradually mapping in the needed hardware. Eventually, the kernel hit the magic start_kernel function in the C code without crashing on the way.

______________________

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState