+=== RB ==
+
+not quite I think. It is more like
+
+ def A_shutdown(cookie, data):
+ ...
+
+ def A_init(data)
+
+ def A_do_stuff(cookie, data):
+ ...
+
+ def A_do_more_stuff(cookie, data):
+ ...
+
+ def B_do_stuff(cookie, data):
+ ...
+
+ def B_shutdown(cookie, data)
+ ...
+
+ interfaceA = {
+ shutdown: A_shutdown,
+ init: A_init,
+ ctl0: A_do_stuff,
+ ctl1: A_do_more_stuff
+ }
+
+ interfaceB = {
+ shutdown: B_shutdown,
+ init: B_init,
+ ctl0: B_do_stuff
+ }
+
+ cpu_interfaces = {
+ 0xABCD: interfaceA,
+ 0x1234: interfaceB
+ }
+
+ cpu_open_handles = {}
+
+ __handleId = 0
+ def new_unused_handle_id()
+ __handle = __handle + 1
+ return __handle
+
+ def ext_open(uuid, data):
+ interface = cpu_interface[uuid]
+ if interface == NIL:
+ raise Exception("Unrecognised interface")
+
+ handleId = new_unused_handle_id()
+ cpu_open_handles[handleId] = (interface, CurrentVirtualMemoryAddressSpace).
+ cookie = A_init(data)
+
+ return (handle_id, cookie)
+
+ def ext_close(handle, data):
+ (handleId, cookie) = handle
+ intf_VMA = cpu_open_handles[handleId]
+ if intf_VMA == NIL:
+ return -1
+
+ (interface, VMA) = intf_VMA
+ if VMA != CurrentVirtualMemoryAddressSpace:
+ return -1
+ assert(interface != NIL)
+ shutdown = interface["shutdown"]
+ if shutdown != NIL:
+ err = interface.shutdown(cookie, data)
+ if err != 0:
+ return err
+ cpu_open_handles[handleId] = NIL
+ return 0
+
+ def ext_ctl0(handle, data):
+ (handleId, cookie) = handle
+ intf_VMA = cpu_open_handles[handleId]
+ if intf_VMA == NIL:
+ raise Exception("unknown interface")
+
+ (interface, VMA) = intf_VMA
+ if VMA != CurrentVirtualMemoryAddressSpace:
+ raise Exception("unknown interface") #Disclosing that the interface exists in different address is security hole
+
+ assert(interface != NIL)
+ ctl0 = interface["ctl0"]
+ if ctl0 == NIL:
+ raise Exception("Invalid Instruction")
+
+ return ctl0(cookie, data)
+
+
+ The other ext_ctl's are similar.
+
+==End RB==
+
+
+
+