Singpolyma

's Profile

Author Archive

Dystoparx — Part 16

Posted on

16:00 <jjdavis> So, we’re all secure now?

16:02 <nicnus> Yup! As long as the devices are on and we use this chat system, everything is being encrypted.

16:03 <jjdavis> That *was* pretty easy. I guess this is a good idea.

16:04 <acklas> 😀

Acklas hits a key combination and is again staring at a screen full of source code. Someone had indeed already worked out what would be needed to spam the RCMP with bogus data, but just being able to shove data down the pipe is not enough. Acklas certainly doesn’t need this backfiring on him. He is not going to run it on his own computer, or over his own Internet connection, and all the traffic needs to be masked no matter where it is he ultimately runs this. Safety first.

He quickly scans over what he’s been writing, makes a quick change, and then tries running the system again. He’s just running it locally to make sure it works, no data is actually being sent.

16:10 <acklas> Ok, I’ve got the MusicBox blocker patched to send whatever data I want

16:10 <acklas> also, it has a backdoor so I can remote control it no matter where it’s running

16:12 <acklas> and I’ve got it working so that it will send all traffic (even DNS) through a given SOCKS proxy

16:14 <nicnus> Tor?

16:15 <jjdavis> good call

16:15 <acklas> yes

Quickly, so that the reader is aware, “Tor” (or TOR, The Onion Router) is a software designed to help whistleblowers and those in oppressive nations use the Internet without fear. It wraps data in many layers of encryption and then bounces it off many different computers, in a way that makes it virtually impossible to tell what computer originally sent the data.

16:17 <jjdavis> Where are you gonna pit it, though?

16:17 <jjdavis> s/pit/put

16:18 <acklas> I’m thinking of renting some space with BTC

16:18 <nicnus> botnet?

16:19 <nicnus> hmm… that’s probably safer

16:19 <nicnus> cool

Acklas begins reading over the list he had brought up earlier of Internet hosting providers that will accept Bitcoin (abbreviated BTC), the pseudonymous digital currency he plans to use as a way to keep his name off of any space he rents. Credit cards and other forms of payment, other than cash, have a tendency to have one’s name attached to every transaction, which is a bad idea if the plan is to send a lot of bogus data to the RCMP, regardless of how you feel about what he is trying to do.

He finds some that seem promising and saves them in a file on his laptop, then packs up to move to the public library. He only lives a few blocks away, so he walks. You may think that such a close library will make it fairly obvious, still, where he lives, and you would be right. Anyone who can trace what he is about to do to the library will have a much smaller pool of people to choose from than they would if they could not trace the connection. However, he has no library card, and does not need one to get online. At a coffee shop, he would likely need to make a purchase to get online, and that’s just that much more data he’d be leaving behind.

Once online, he sets up his Tor client to further mask the source of his data. He scans the list of Internet hosts he has drawn up and keys in an email to each one, asking for some details and requesting that they reply by emailing certain email addresses he has selected at Mailinator, the free service that provides disposable email addresses to anyone. Why does he have to ask for them to email these disposable addresses instead of just replying? He is sending the emails through a service called “Mixmaster” which hides the email address of the sender. The people receiving these emails won’t be able to tell who Acklas is or where he was when he sent the email, yet they can still reply.

If they are willing to do that, then he will know they are the right sort of place for what he has in mind.

Bill is happy. He’s not sure what the exact status of the politics are, but the media seems on board. The most recent operation was just a start. They need more data. Whatever it takes to stop the abuse of children. Whatever it takes.

There is a knock at his office door. He does not hear it. He is, of all places, on Wikipedia, reading about Internet technologies. He’d really prefer some other source, but this seems to be the only place with the information he’s interested in. If they’re going to take down the pornographers, he needs to have some understanding of what’s possible. It’s no good asking for data they cannot possibly get, or not asking for data that would be easy.

Another knock, and the door opens. One of the cops who works with him. One of the more technical ones. Under him. With him. Whatever, the distinction is lost on Bill most of the time. Apparently there’s something interesting they’ve found in the data. Something worth seeing.

Bill is lead to a small meeting room. Others are already there. They wanted to be more sure before presenting this to him. They are showing him data. Some of this data is disgusting. More explicit than anything they’ve yet uncovered. As he is shown more and more data Bill is wondering, can they catch this guy? What a windfall!

It turns out they’re way ahead of him. He has a good team. They traced the traffic, but they didn’t really need to. This is the good stuff. The user was logged in to MusicBox and everything when the pornography was downloaded (and uploaded! A source!) They have his name, his credit card information, his address. Everything.

Oh.

Oh.

“You did WHAT?”

Acklas is smiling. It has been a good few days. “I set up some services to send fake data through the MusicBox service.”

“Yes, I got that part,” Nicnus looks around worriedly. They are in private, but this sort of discussion makes him even more paranoid than usual. No one out the window, at the least. “Repeat the other part.”

“I set one of them in motion streaming fake kiddie porn downloads and uploads for the RCMP Commissioner.”

Nicnus blinks. “You did WHAT?”

“I think we’ve been here already.”

Nicnus pauses for a long moment, then, “Are you crazy?”

Acklas shrugs, “Quite possibly. Still, I was very careful. The traffic should be untraceable, and even if they find the box it isn’t tied to me.”

“No, I know,” Nicnus stares at the wall for a bit.

“It seems reckless.”

“Yes.”

“Then… ?”

Acklas sighs. “I’m just sick of it all. Writing MPs, signing petitions, reading proposed law, dealing with my boss, going to meetings—”

“You can’t blame your crappy ex-job—”

Acklas shakes his head, “No, I know. Look. I know. I just mean. Yeah. Hoops. No more hoops. I’m taking the direct approach.”

Nicnus sucks on his teeth a bit. This is a hard one. Nicnus also hates jumping through hoops. He also hates the feeling of powerlessness the current political climate gives him. He hates seeing the world slowly and willingly slip into madness. This is how he feels, and he understands how his friend feels. Nothing, however, is airtight. There’s a huge risk in this. What will this accomplish anyway?

He verbalizes that last thought.

Acklas is about to respond when both their phones go off.

piratepartyca: BREAKING: RCMP Commissioner temporarily relieved pending investigation.

Nicnus raises an eyebrow at Acklas.

Acklas looks up and meets his gaze. “I was going to say that what happens depends on how they respond, but I guess we know that now.”

Nicnus is shaking his head already, “You know you may have destroyed an innocent man’s life, right?”

Acklas holds up one hand in a defensive gesture, “Woah, I’m not just going to leave him! I’m going to tell them what I’ve done.” He sees Nicnus about to make a response, “Anonymously, of course. I’m being careful. Isn’t this exactly the idea you brought up when we first saw mediacrack?”

Nicnus thinks back. That seems so long ago. “Yes. I suppose it is. I didn’t expect anyone to do it, though. That’s why I built a blocker instead.”

“I know. But this way they’ll have to see how unreliable the data is. After their disaster operation recently, and then this, they’ll have to see how futile this is.”

Nicnus is thinking. It can’t be that easy. “The data is only unreliable if people keep sending bad data. They’ll just focus their attention on you and anyone else trying to do this.”

Acklas sighs. “Maybe. And maybe they shut some of us down. And then someone even shadier than me puts it on the botnets and then shutting it down becomes as hard as fighting SPAM. Believe me, I questioned the ethics of this myself, but someone has to do it. This has all gone on far too long. I will push them until they break. The whole frigging media industry, every moron bureaucrat at every level, if it comes to it. We can’t just keep hoping that letters and petitions and books and blog posts will eventually make them see the light. The time for polite suggestions is over. They need to be stopped.”

Nicnus shifts uncomfortable, but gives in, “I think it’s very risky, and I’m much less convinced it will be successful than you are, but you’re right about one thing: the whole situation has gone on far too long. I wish you luck.”

Their phones once again buzz as one. It’s time for them to head out towards the meetup they are attending tonight.

Jack is at home again. Rested. Worried. Rested and worried. Now that he’s had some time for everything to sink in, he’s begun to realise the full import of his situation. He can’t just ignore what he found, because now others are finding it too. Based on his reports online, several other systems and network administrators have identified the same spying going on in their datacentres. Some of them have been much more ruthless in getting rid of it. Others have also traced it to variously the FBI, the US military, or the Whitehouse. Jack’s guess is that the IP blocks overlap quite a bit. What this does indicate, however, is that it’s not a security breach at the US government. They’re monitoring on purpose. And it’s illegal.

Sure there are all sorts of debates going on in Congress right now about how much monitoring should be allowed, but none that have gone much of anywhere. Unless the FBI has just decided to treat everyone they tap as terrorists… but they seem to be tapping indiscriminately.

Jack hopes the wealth of reports will take the heat off himself. Maybe some of these more rash admins will get the call.

He has an appointment set up with a lawyer. Just in case he gets the call and it’s the sort of call where they let you talk to a lawyer. He wants to know one. And have the phone number tattooed on his arm. He is also considering some sort of dead-man’s switch to notify people if he disappears unexpectedly.

One thing is for sure. He’s not leaving.

Haskell for Rubyists

Posted on

In the last year I’ve been playing with a new language: Haskell. I have found it to be a very suitable second high-level language for me as a Rubyist, and in this post I will explain (with examples!) some of why I, as a Rubyist, love Haskell, and how you can use it to do awesome things.

Why Another Language?

One thing I wasn’t sure about for a long time was if I even needed another language. Obviously, being able to work in any environment you have thrown at you is an essential job skill, but I mean a language I chose for myself. I am a Rubyist, do I need to be anything else?

Well, while I love Ruby, there are a few reasons I eventually chose to go in search of a second language to call my own:

  1. Performance

    Ruby implementations are getting faster all the time, but we all know there are faster things out there. It’s the reason some things (like the JSON gem) have versions written in C. I felt it would be nice for some tasks to get the performance boost that comes from an optimising compiler, without having to drop all the way to C.

  2. Portability

    Yes, Ruby is super-portable… to systems that have a ruby implementation. It’s somewhat complex to just email arbitrary users a ruby script and hope they can use it without setup help.

  3. Linking with native code

    Ruby extensions and FFIs exist so that we can call C code from Ruby. What about calling Ruby code from C or another language? It can be done, but only if most of MRI is linked in to the target and the Ruby is more-or-less “eval’d”.

In case you haven’t guessed, basically I wanted a nice high-level environment like Ruby, but with a native code output.

Isn’t Haskell Hard?

No. Or at least, not harder than any other language. It is true that the Haskell community has a higher-than-average concentration of Ivory Tower Dwellers. Yes, some of them have been in the Tower for so long that they have forgot how to write anything but symbols from higher-order logics. Yes, the documentation for some really nice Haskell libraries and features are dense academic papers. Don’t let them scare you off. There are humans in the community as well, and # on freenode IRC has many of them.

Type Inference

One of the nice features of Ruby is the type system. If you’re used to un-inferred static typing (read: C) then the ability to write code like this:

def fun(a, b); (a + b) * 3; end

is liberating. Haskell has a static type system, which means that you’ll never have a program crash in production because you’re passing in different data than you though, but only in a case your tests didn’t catch. Unlike C, however, Haskell’s system is strong (which means that data is not magically cast for you, so you get stronger guarantees, just like how in Ruby we must write 1.to_s + "hello" not 1 + "hello"), but more importantly it is inferred, so the equivalent of the above in Haskell is:

fun a b = (a + b) * 3

You can add type annotations (like in C) if you want to, which sometimes helps for clarity, but you don’t need to.

The only limitation here is that data structures are mostly of a single type, for example in Ruby:

a = [1, "hello"]

is perfectly fine. This is sometimes a good thing, and sometimes causes strange bugs. In Haskell, this would be an error, so we need to define unions explicitly:

data StuffInMyList = I Integer | S String
a = [I 1, S "hello"]

A small pain, but I feel it’s a fine trade-off.

Mixins

The mixin module is one of the defining characteristics of Ruby. Haskell has something similar, called Typeclasses, which form the foundation of polymorphism in the language. In Ruby:

module Equality
def equals?(b); self == b; end
end

class Thing
include Equality
end

In Haskell:

class (Eq a) => Equality a where
	isEqual :: a -> a -> Bool
	isEqual x y = x == y

data Thing = Thing deriving (Eq)

instance Equality Thing

This looks a bit different. You’ll note I had to give a type signature to the isEqual function. This is one of the few places you have to, and it has to do with making the polymorphism we get with mixins a bit safer. My Equality mixin has to be restricted to types from the Eq typeclass (because I use == on them), which is also true in Ruby except that in Ruby every single class has == defined.

Significant Whitespace

Haskell has significant whitespace. If you’re a Rubyist on the run from Python this may scare you, but there are two reasons this does not bother me. First, the Haskell whitespace is much nicer than in Python, and the way code gets written in Haskell you rarely have the “where does this huge block end?” problem. Second, the whitespace in Haskell is optional! Here’s that typeclass again, but without the whitespace use:

class (Eq a) => Equality a where { isEqual :: a -> a -> Bool; isEqual x y = x == y; }

Great!

Let’s see a real example!

You may have heard that Haskell I/O is weird, and that Haskell has no access to mutation. While Ruby code is often non-destructive in nature itself, access to mutation is sometimes handy. Understanding why Haskell I/O is safe and such is not terrible, but it does take learning a new concept (called Monads, with roots in those academics, but there are good simple explanations out there without too much math, like in Learn You a Haskell (for Great Good), which I recommend), but doing simple I/O is actually not complicated.

main = do {
text <- readFile "somefile.txt";
print $ length $ lines text;
}

This is the Haskell code to read a text file, split it in to lines, count the number of lines, and print out that number. Pretty simple!

What about mutation? Well, it is true that there are no globals in Haskell, but really, who uses globals? If you really need mutation for something, the simplest way to make a reference is:

import Data.IORef

main = do {
someRef <- newIORef 1;
val <- readIORef someRef;
print val;
writeIORef someRef 12;
val <- readIORef someRef;
print val;
}

Of course, if you want you could make this a bit less verbose:

import Data.IORef

x := y = writeIORef x y
new x = newIORef x
get x = readIORef x

main = do {
someRef <- new 1;
val <- get someRef;
print val;
someRef := 12;
val <- get someRef;
print val;
}

Many Libraries

Haskell has a very active community that has produced many libraries covering all sorts of cases. The main place to look for these is Hackage.

REPL

Another thing that drew me to Ruby initially was irb. The ability to just fire up a shell-like environment and enter expressions, and load in my code and play with it live, is a very nice thing. There are several such environments for Haskell, the one that I prefer is GHCI, which also has commands to set breakpoints and such (which I have never needed) and to find out what the type of some expression is (very handy).

Other Useful Bits

There is a very useful tool for Haskell called hlint, which analyses your code and make (sometimes surprisingly insightful) suggestions. I don’t always agree with it, but it is very nice.

Debug.Trace is a very useful library for printing out arbitrary values from anywhere in your code without otherwise affecting the behaviour of the code. Very useful for debugging.

If you want to learn more, I highly recommend Learn You a Haskell for Great Good.

Writing a Simple OS Kernel — Part 5, Hardware Interrupts

Posted on

Last time we got the kernel switching back and forth between multiple tasks. This time we’re going to get a simple timer to wake up the kernel at fixed intervals.

Motivation

So far we’ve been interacting with hardware when we want to. Writing to the serial port when we have something to print, for example. Why would we want to let the hardware interrupt what we’re doing?

A few reasons:

  1. Currently, our user mode tasks have to call a syscall on purpose in order for any other task to be able to run. This means one task can run forever and block anyone else from running (called starvation). So we at minimum need some way to interrupt running tasks (called preemption).
  2. The hardware knows when it’s ready. It is way more efficient to let the hardware say “I’m ready now” then to keep asking it.
  3. The hardware may actually stop being ready by the time we get around to talking to it again. We want to talk to the hardware during the window when it’s able to talk

Enabling a Simple Timer

Before we get to the serial port, there is an even simpler piece of hardware we can work with first: timers. Timers just take a value and count down to zero, sort of like an alarm clock.

I could make you go look up in the user guides what all the magic numbers for our timers are, but I’m a nice guy, so here’s the ones we need to add to versatilepb.h:

/* http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0271d/index.html */
# TIMER0 ((volatile unsigned int*)0x101E2000)
# TIMER_VALUE 0x1 /* 0x04 bytes */
# TIMER_CONTROL 0x2 /* 0x08 bytes */
# TIMER_INTCLR 0x3 /* 0x0C bytes */
# TIMER_MIS 0x5 /* 0x14 bytes */
/* http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0271d/Babgabfg.html */
# TIMER_EN 0x80
# TIMER_PERIODIC 0x40
# TIMER_INTEN 0x20
# TIMER_32BIT 0x02
# TIMER_ONESHOT 0x01

Now add the following somewhere near the top of your main:

*TIMER0 = 1000000;
*(TIMER0 + TIMER_CONTROL) = TIMER_EN | TIMER_ONESHOT | TIMER_32BIT;

The timer on our QEMU system defaults to using a 1Mhz reference, which means that it ticks 1000000 times per second. So, by setting it’s value to 1000000, it will reach 0 after one second.

Then, on the second line, we enable the timer, set it to “oneshot” mode (that is, it will just stop when it’s done counting, we’ll see another mode later), and set it to 32BIT mode (so that it can hold really big numbers).

Now, somewhere in your while loop, put this:

if(!*(TIMER0+TIMER_VALUE)) {
   bwputs("tick\n");
   *TIMER0 = 1000000;
   *(TIMER0 + TIMER_CONTROL) = TIMER_EN | TIMER_ONESHOT | TIMER_32BIT;
}

We’re just printing out “tick” if the timer has reached 0, and then resetting and re-enabling the timer.

Build and run your kernel. It should still work as before, but now it will also print out “tick” once per second.

Back to the Vector Interrupt Table

The CPU uses the same mechanism for hardware interrupting operation (often referred to as an “interrupt request” or IRQ) as it used know where to jump when we used svc. We need to add an entry to the right place. Here’s the new code for bootstrap.s:

interrupt_table:
ldr pc, svc_entry_address
nop
nop
nop
ldr pc, irq_entry_address
svc_entry_address: .word svc_entry
irq_entry_address: .word irq_entry
interrupt_table_end:

IRQ entry point

Back over in context_switch.s, irq_entry is going to be very similar to svc_entry, but with some changes to handle an IRQ instead of a syscall.

As soon as we enter system mode, we need to push the value of r7 onto the task’s stack (because that’s what the syscall wrapper does, and we need to set things up to look the same). The syscall puts the number identifying the syscall into r7, so we should come up with something that will identify an IRQ and put that there instead. To make sure the number never clashes with a syscall number, lets use negative numbers:

push {r7} /* Just like in syscall wrapper */
/* Load PIC status */
ldr r7, PIC
ldr r7, [r7]
PIC: .word 0x10140000
/* Most significant bit index */
clz r7, r7
sub r7, r7, #31

That value at PIC is the address of the interrupt controller status (that is, the value that can tell us which interrupts are currently firing) and then we just do some operations on that value to find out what the most significant bit set in the value is, and transform that into a negative index to use for the identifier of this IRQ.

The only other weird thing is that we have to get the value of the lr from IRQ mode instead of Supervisor mode, and the value is set to the instruction after the one we should be returning to, so we’ll need to subtract 4:

msr CPSR_c, # /* IRQ mode */
mrs ip, SPSR
sub lr, lr, # /* lr is address of next instruction */
stmfd r0!, {ip,lr}

So those bits, together with what we already had from svc_entry give us this complete entry point code:

.global irq_entry
irq_entry:
	/* Save user state */
	msr CPSR_c, # /* System mode */

	push {r7} /* Just like in syscall wrapper */
	/* Load PIC status */
	ldr r7, PIC
	ldr r7, [r7]
	PIC: .word 0x10140000
	/* Most significant bit index */
	clz r7, r7
	sub r7, r7, #31

	push {r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,fp,ip,lr}
	mov r0, sp

	msr CPSR_c, # /* IRQ mode */
	mrs ip, SPSR
	sub lr, lr, # /* lr is address of next instruction */
	stmfd r0!, {ip,lr}

	msr CPSR_c, # /* Supervisor mode */

	/* Load kernel state */
	pop {r4,r5,r6,r7,r8,r9,r10,fp,ip,lr}
	mov sp, ip
	bx lr

A Small Problem

We’re pushing the value of r7, but where is it going to get popped? The syscall wrappers do both the push and the pop, but in the case of an IRQ we’re pushing in the entry point, and we have no easy way to tell activate that it’s exiting from an IRQ instead of a syscall. Hmm.

The solution to this that I’ve chosen is to remove the pop instruction from the syscall wrappers, and add an identical instruction to right after the pop instruction already in activate. Now the pop always happens.

More Magic Numbers

We hardcoded a single magic number into our assembly, but luckily that’s the only one our assembly code will need. In fact, we should not need any more assembly! (Unless we wanted to implement other sorts of hardware exceptions, which we may get to.) We do need that and a few other interrupt-controller related magic numbers in our C code too, so here’s the stuff for versatilepb.h:

/* http://infocenter.arm.com/help/topic/com.arm.doc.dui0224i/I1042232.html */
# PIC ((volatile unsigned int*)0x10140000)
# PIC_TIMER01 0x10
/* http://infocenter.arm.com/help/topic/com.arm.doc.ddi0181e/I1006461.html */
# VIC_INTENABLE 0x4 /* 0x10 bytes */

Enable the Interrupt

We have to tell the timer to generate interrupts, and we have to tell the interrupt controller to pass those interrupts through. So, replace your current one-shot timer code with this:

*(PIC + VIC_INTENABLE) = PIC_TIMER01;

*TIMER0 = 1000000;
*(TIMER0 + TIMER_CONTROL) = TIMER_EN | TIMER_PERIODIC | TIMER_32BIT | TIMER_INTEN;

Note that we’re also using a new timer mode here. Periodic mode will automatically cause the timer to restart from the value we set when it hits 0, which is perfect for when we’re getting an interrupt instead of waiting until the value hits 0.

Handle the Interrupt

Now we have to add a case to our switch statement for this request:

case -4: /* Timer 0 or 1 went off */
	if(*(TIMER0 + TIMER_MIS)) { /* Timer0 went off */
		*(TIMER0 + TIMER_INTCLR) = 1; /* Clear interrupt */
		bwputs("tick\n");
	}

The way our hardware is set up, the interrupt is actually the same for either timer 0 or timer 1. Now, we know that we’ve only set timer 0, but that’s lazy. TIMER_MIS is the offset where we’ll find a flag telling us if Timer0 was the one that is causing the interrupt. After checking that, we set a value on the timer to clear the interrupt (basically to say that we’ve seen it so that it won’t call us again unitl the next interrupt). Finally, print out “tick”.

As it is, this code should operate exactly the same as the version without the interrupts.

Pre-emption

Up until now, we’ve needed the dummy syscall syscall in an infinite loop at the end of every task in order to let other tasks run. Since the timer interrupts the tasks now, we don’t need them to interrupt themselves. Remove all references to the syscall syscall and try running the kernel again. Now you’ll note that the first task just stays in its infinite loop and does not let the other task keep running. Does not, that is, until the timer goes off and you see “tick” printed for the first time, after which the tasks switch. So the tasks are now being properly interrupted even if they do not give up control!

We’re done!

The kernel now has all the machinery it needs to be able to handle hardware interrupts, and tasks are being pre-empted by the timer. Next time we’ll start looking at IPC.

The code so far is on GitHub.

Dystoparx — Part 15

Posted on

Jack is outside her apartment. He slipped into the building behind someone else so that he wouldn’t have to push the buzzer. Now he’s just standing here.

He left the datacentre mostly under control. One of the ops came in and got a run-down of what’s going on. Just keep the place running, that’s all the op has to do. The attack should be basically harmless at this point.

Well, harmless to the datacentre.

Jack’s name is now directly associated with the attack as “the guy who first reported it.” Not good if his suspicions are correct. Maybe Nicnus is right, maybe he should—

Her door opens. It’s her. Of course, who else would it be?

She seems a bit surprised, “You came?”

“Uh…” He’s not sure he was being expected, but, “Yes?”

She smiles a sort of sad smile, then, “Come in.”

He comes in. Not sure where this is going. Maybe her friend told her something? Maybe he’s caught in some sort of unclear set-up? Best to just wait it out for now. So far she’s not unhappy with him.

Now he’s sitting in her living room. She’s in the kitchen. Probably preparing beverages. As ridiculous as it sounds, in this particular case he likes it. It’s a welcome form of procrastination.

He feels his phone vibrate. No. His attention is on her now. It vibrates again. No.

She is back with the beverages. On the coffee table. Now she is across from him, sipping. Looking expectant. Crap. He probably should have had a plan. Or… he thinks back over his conversation with her friend and decides to try something.

“So… I have absolutely no plan here.”

She says nothing, but at least she does not exhibit any signs of being upset by this. He decides to sip his drink and wait. The mug contains some sort of tea he could never get the name right for. It’s her favourite, and really not his sort of thing. He sips anyway.

His phone vibrates again. He is still ignoring it. Sipping. Willing his mind not to drift elsewhere. Here is where he needs to be right now. All of him.

“I’ve missed you.” She says it like it should be obvious.

“I thought you didn’t want to see me.”

She shakes her head in disgust, “That’s always been your problem.”

He raises an eyebrow at her and says nothing. Either an explanation is coming, or else he’s going to have to make it more obvious that he needs one.

“Why did you think I didn’t want to see you?”

He sips for a few moments, as if he is thinking. She needs to see him thinking. Finally, “You said as much.”

“Did I?”

He nods.

“Probably. Sounds like something I might have said. Your problem is that you take everything at face value.” She tosses her hair and continues to sip her tea.

He’s quite impressed. They’re having what is effectively a mostly-rational discussion about what happened. He never would have expected this. As he takes his next sip he realises something: this is his problem. He doesn’t give her enough credit. That she should be discussing the situation with him in a rational manner should not be impressive.

Yet…

Nor should it be expected. Huh.

“Because you didn’t want to see me then.” He says this slowly, as it comes to him, “But you didn’t want to have to tell me when that changed. Didn’t want to, because it seemed obvious that it would change somewhat quickly. You thought I should know that you would eventually want to see me.”

She is saying nothing, but her eyes say all he needs to know. His phone vibrates again.

“I know I can be hard to handle,” he pays his phone no mind, “I mean, I don’t take the world lying down. I expect clarity in communication. I want to correct errors wherever I see them—” He cuts himself off, a list of his faults is not going to help here. She knows what her problem is. “I can relax all that,” He’s sure that he can, “I mean, I can be more understanding. I can try to read into what you’re saying just a little more. Let trivialities be trivial a little bit more often…”

He doesn’t want to say what he has to say next. Things are going so well. He is realising, though, that as much as he wants this to work, it is not his whole world. So he says it. “I’m not going to change who I am, though. I’m not going to stop analysing the world. I’m not going to instantly know your every wish. You still have to tell me things. More than once. More clearly. Maybe even more than you would normally have to with other guys. Because I’m not them.”

His phone is vibrating constantly now, as though a phone call is coming in.

She finally sets her tea down and speaks, “You’re right. This isn’t all your fault, and I can try harder. Your willingness to take the first step is all I really needed.” She pauses for a moment, then shakes her head, “Now answer your phone before you go insane.”

Bill is not sure how he feels. The operation was completely successful, and yet it had been a colossal failure. Every single person they had set out to question had been easily found and questioned. None had evaded them, none of their information had led them to a dead-end. By operational standards, a resounding success.

Not a single person they had questioned, however, had ties to child pornography groups. At least, not so far as they could discern. Not a one. Well, except for the teenagers they had caught with pictures of girlfriends and boyfriends. Hardly the sort of contraband they were after.

What had gone wrong? The FBI had nailed a guy based on their surveillance, how could a large-scale operation here fail to be at least a little successful?

The techies told him it was the nature of the surveillance. People with MusicBox knew it was tapped. They were unlikely to store their pornography on the same machine. Bill knew something, however, that the techies did not know. Data was also being piped in from email and web histories in other ways. Ways which Bill did not himself understand. Then again, some of the techies knew some of these things.

Bill is now looking over the data he has on the successful FBI operation. The one that convinced him that this could work. The pervert had sent an email to his mother, that’s how they had found him. They had picked up the email on his mother’s machine.

Suddenly, Bill has an insight. They had been looking for suspicious material, and tracking that material to the machines it had passed through. Those machine were accessed who knows how, or maybe the wifi connections associated with the machines were compromised, whatever. The success with the FBI had been tracking activity from someone suspicious to a known connection, then back-tracing the activity to the real source somehow. The technical details don’t matter. Bill doesn’t really understand them. The strategy matters. Stop spying only on suspicious data, and start trying to correlate it with otherwise innocent data!

This is great. This can work. He cannot task anyone to do it, however, until the politics gets sorted out. To ask anyone to do this would be to admit that they were not only interested in raw MusicBox data. With the right spin, the operation could still provide the media bomb they need.

He dials his supervisor.

Acklas hangs up as soon as Jack answers. Soon Jack has caught up on the messages they’ve been sending him and begins to reply.

14:00 <acklas> So, I just quit my job.

14:02 <nicnus> Awesome?

14:02 <acklas> nicnus: I’ll let you know. I don’t suppose there are openings at your startup?

14:03 <nicnus> We’re “always hiring”, so maybe.

14:05 <nicnus> Woah! Have you seen this thing about the kiddie porn arrests?

14:06 <acklas> Just a sec…

14:10 <acklas> Woah. That is not how it seemed to be going down at all…

14:12 <nicnus> You mean the part about how this shows there is “more of this problem than we, as a society, want to admit”?

14:12 <acklas> Yeah. From what I understood, it was mostly a junk operation. Scaring old ladies and single mothers, instead of doing their jobs.

14:13 <nicnus> Apparently, they’d rather not remember it that way.

14:15 <acklas> Apparently, they’d rather ask for sweeping surveillance provisions. Because, you know, this proves they can totally handle it.

14:15 <nicnus> No, this proves they might be able to scare people into thinking we need it.

14:15 <acklas> Suresure. Man, I found one article that says the USA “has been doing something similar for some time”

14:16 <nicnus> First I’ve heard of it. I think it’s more that they’re also *trying* to get something in place.

14:16 <acklas> Yeah. The security community would have seen something by now if they were doing it before the laws were in place.

14:20 <nicnus> Jack!

14:20 <acklas> What?

14:20 <nicnus> jjdavis: What, exactly, did you stumble on again? The US gov’t snooping emails?

14:21 <acklas> Woah, when did this happen?

14:21 <nicnus> During your meeting.

14:22 <acklas> jjdavis: is this true?

14:23 <nicnus> jjdavis? This is a big deal, man. You can’t stay down there.

14:24 <acklas> Yeah, if they’re spying semi-illigally…

14:24 <nicnus> If it’s not even legal… yeaah

14:35 <nicnus> jjdavis?

14:40 <nicnus> Oh my goodness, they’ve got him.

14:45 <acklas> dan’t be silly. he’s at his girl’s, remember?

14:50 <acklas> Ok, I’ll call him. You know. He should at least know about this.

** jjdavis reads

14:57 <jjdavis> nicnus: like I said, I’m not going anywhere. I’ll be ok

14:58 <acklas> Sure, I mean, nicnus is a bit paranoid. Still, though…

14:59 <jjdavis> From the way that article reads, it’s about to get hot up there as well.

15:00 <nicnus> maybe, but you didn’t *find* them up here.

15:01 <jjdavis> We’ll see

15:03 <acklas> How did it go with your SO, anyway?

15:05 <jjdavis> I’m still here. We’ve sort of patched things up in principle.

15:06 <acklas> I guess that’s good, then.

15:07 <nicnus> jjdavis: I’m overnighting you one of our devices, set with a key I’ll give to acklas as well.

15:09 <jjdavis> nicnus: sure

15:16 <acklas> nicnus: how hard would it be to modify your MusicBox blocker to instead spam the crap out of the RCMP?

15:18 <nicnus> Pretty easy. I think someone already did it in principle. why?

15:20 <acklas> I’ll get back to you on that.

Acklas is thinking. If the States is already on it, then Canada might not be so far behind. There needs to be resistance to the surveillance before it becomes law. After that, everything just gets harder. Even Nicnus’ magic crypto doesn’t stop spyware. Doesn’t stop email snooping. At least, not by itself.

If they were going to use their failure to tell the media why they needed more power (“they” in this case being some nameless force Acklas paints as being behind the whole mess), then Acklas could maybe use something else to sway the media another way. A battle in the media might be a battle that could be won.

Why has he not thought of this before? Man. Half a day out of the cubicle and already his creative problem-solving juices are flowing again. He has nothing else that he needs to be doing, and money is not going to be an issue for awhile.

If this is what’s necessary to keep from living in fear, then Acklas will bring the fight.

Writing a Simple OS Kernel — Part 4, Multitasking

Posted on

Added a description of fork, 2012-36.

Last time we got our kernel switch back and forth between a user mode task. The focus in this post is going to be adding the ability to run multiple tasks.

Hack Two Tasks in There

Our kernel currently has no way for tasks to exit, so we need to make sure they never try to return, since that will probably cause an error and hang the system (in the best case). So change the syscall invocation at the end of first to be in an infinite loop, such that if the task gets re-activated after it is done it just immediately calls back into the kernel.

Then, add a second function so that we have something else to run:

void task(void) {
	bwputs("In other task\n");
	while(1) syscall();
}

We’re going to run two tasks, so change the first_stack and first_stack_start to something like:

unsigned int stacks[2][256];
unsigned int *tasks[2];

And setup both tasks, in basically the same way as we did before:

tasks[0] = stacks[0] + 256 - 16;
tasks[0][0] = 0x10;
tasks[0][1] = (unsigned int)&first;

tasks[1] = stacks[1] + 256 - 16;
tasks[1][0] = 0x10;
tasks[1][1] = (unsigned int)&task;

Then, call activate with tasks[0] and tasks[1] instead of first_stack_start. Recompile and run, you should see both tasks running in the order that you activate them.

Some abstractions

We’ve hardcoded a bunch of stuff again. We should clean it up so that we don’t have to keep copying these chunks every time we want a new task. Let’s move those magic numbers into constants and make a function to set up a new task:

# STACK_SIZE 256 /* Size of task stacks in words */
# TASK_LIMIT 2   /* Max number of tasks we can handle */

unsigned int *init_task(unsigned int *stack, void (*start)(void)) {
	stack += STACK_SIZE - 16; /* End of stack, minus what we're about to push */
	stack[0] = 0x10; /* User mode, interrupts on */
	stack[1] = (unsigned int)start;
	return stack;
}

Then, back in main clean up the task initialisation to use these abstractions:

unsigned int stacks[TASK_LIMIT][STACK_SIZE];
unsigned int *tasks[TASK_LIMIT];

tasks[0] = init_task(stacks[0], &first);
tasks[1] = init_task(stacks[1], &task);

Scheduling

Scheduling (deciding what tasks to run, when, and for how long, in a multitasking system) is a big topic. We are going to implement the simplest possible (in our situation) schedule: round-robin. This means that we’ll just activate each task in the order that they were created, and then go back to the beginning and activate each in sequence again.

Since we have no way for tasks to exit, make sure the following is at the end of each task:

while(1) syscall();

This will just cause the task to repeatedly call back into the kernel when it is done.

We’ll also need to keep track in main of how many tasks there are and which one we’re currently running:

size_t task_count = 0;
size_t current_task = 0;

In order to use size_t you’ll need to include stddef.h. Now just make sure that task_count gets set correctly, and replace the code that activates user tasks with this:

while(1) {
	tasks[current_task] = activate(tasks[current_task]);
	current_task++;
	if(current_task >= task_count) current_task = 0;
}

This just activates each task in order, repeatedly, forever. Fairly simple.

You may be wondering why I didn’t use the classic current_task = (current_task + 1) % task_count trick. The reason is that, at least on ARMv6, gcc actually generates a library call for the % operator. You could try linking in libgcc, but there are some problems. For this case, it just wasn’t worth it, and so I’m using an if statement instead.

Code so far on GitHub

Setup for new Syscall

So, our syscall syscall is cute, but it’s not very useful. For good multitasking, we want a way for a user mode task to create new tasks. We’ll do this the way Unix does: fork. This syscall copies the current process, and then returns the ID of the new process to the parent, and 0 to the child.

We need a way for the kernel to know what the user mode task wants it to do. As I mentioned in a previous post, this is what the “argument” to svc is for. We could add code to our context switch to mask the bits off of the end of the svc instruction, but let’s not bother our context switch now. We’re going to store the id of our syscall in a register. syscalls.s is now:

.global syscall
syscall:
	push {r7}
	mov r7, #
	svc 0
	pop {r7}
	bx lr

.global fork
fork:
	push {r7}
	mov r7, #
	svc 0
	pop {r7}
	bx lr

That’s two syscalls, and the context switch will save the new value of r7 on the top of the stack, which we can read in the kernel. The actual value of r7 gets saved and restored by the syscall wrapper.

In order to call our new syscall, we’ll need to add the following to asm.h:

int fork(void);

memcpy

As you may have guessed, we’re going to implement the fork syscall, in order to make our multitasking more actually useful. To do that, we’re going to need to copy stuff. Because we’re not linking in libc at this point, we need our own memcpy, like so:

void *memcpy(void *dest, const void *src, size_t n) {
	char *d = dest;
	const char *s = src;
	size_t i;
	for(i = 0; i < n; i++) {
		d[i] = s[i];
	}
	return d;
}

Forking

We’re going to use fork to spawn task from first. So, move task up above first, and replace the first call to syscall with:

if(!fork()) task();

Then, modify main so that only first gets set up to run.

The Actual Syscall

So, we’re jumping into the kernel now, but so far we don’t actually do anything with this new syscall. How can we even tell which syscall is being called? Well, remember that the syscall id is in r7 when the context switch happens, so by the time we get to the kernel, we can access it like so:

switch(tasks[current_task][2+7]) {
	case 0x1:
		bwputs("fork!\n");
	break;
}

What’s that 2+7 for? Well, remember, we push SPSR and the supervisor mode lr on the top of the stack, so we have to move past those to get to the registers.

Our kernel has a limitation. Specifically, TASK_LIMIT. If we call fork when there is no space for a new task, that would be bad, so we should return an error:

if(task_count == TASK_LIMIT) {
	tasks[current_task][2+0] = -1;
} else {
}

What are we doing here? We’re changing the saved value of r0, which, you will recall, is the return value of a function. So, we are setting the return value that the user mode task will see to -1.

Now, what does fork actually need to do? It copies all the state from one task into a new one. The new task will need a stack pointer, and said stack pointer will need to be exactly as far from the end of the stack as the current task’s stack pointer:

size_t used = stacks[current_task] + STACK_SIZE - tasks[current_task];
tasks[task_count] = stacks[task_count] + STACK_SIZE - used;

Now we actually have to copy the stack over. Luckily, we know exactly how much to copy, since it’s the same as the distance from the stack pointer to the end:

memcpy(tasks[task_count], tasks[current_task], used*sizeof(*tasks[current_task]));

fork is specified to return the new PID in the parent process, and 0 in the child process:

tasks[current_task][2+0] = task_count;
tasks[task_count][2+0] = 0;

And, finally, we should probably record the fact that there’s a new task:

task_count++;

That’s it!

We now have a multitasking kernel (remember to increase TASK_LIMIT if you want to run more tasks!), and user mode tasks can start new tasks. Next time: hardware interrupts!

Code for this post is on GitHub.