// orb/invoke.cc -- Method Invocation
//
-// This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
+// This software is copyright (c) 2007 Scott Wood <scott@buserror.net>.
//
-// Permission is hereby granted, free of charge, to any person obtaining a copy of
-// this software and associated documentation files (the "Software"), to deal with
-// the Software without restriction, including without limitation the rights to
-// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
-// of the Software, and to permit persons to whom the Software is furnished to do
-// so, subject to the following conditions:
+// This software is provided 'as-is', without any express or implied warranty.
+// In no event will the authors or contributors be held liable for any damages
+// arising from the use of this software.
//
-// * Redistributions of source code must retain the above copyright notice,
-// this list of conditions and the following disclaimers.
-//
-// * Redistributions in binary form must reproduce the above copyright notice,
-// this list of conditions and the following disclaimers in the
-// documentation and/or other materials provided with the distribution.
-//
-// * The names of the Software's authors and/or contributors
-// may not be used to endorse or promote products derived from
-// this Software without specific prior written permission.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
-// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
-// SOFTWARE.
+// Permission is hereby granted to everyone, free of charge, to use, copy,
+// modify, prepare derivative works of, publish, distribute, perform,
+// sublicense, and/or sell copies of the Software, provided that the above
+// copyright notice and disclaimer of warranty be included in all copies or
+// substantial portions of this software.
#include <kern/types.h>
#include <kern/orb.h>
#include <kern/pagealloc.h>
#include <kern/compiler.h>
+#include <kern/thread.h>
+#include <kern/process.h>
+#include <kern/arch.h>
+#include <kern/lock.h>
+
#include <arch/usercopy.h>
+
+#include <util/misc.h>
#include <orb.h>
using System::RunTime::ParamInfoBlock;
+typedef ParamInfoBlock::Segment Segment;
using namespace ORB;
using Threads::Thread;
+using Util::round_up;
namespace ORB {
static CallFrame *new_frame(Thread *thread)
return &hdr->frames[0];
}
- return &hdr->frames[thread->orbstack_top += 1];
+ return &hdr->frames[++thread->orbstack_top];
}
-}
+
+ Object *IDSpace::lookup(ID id)
+ {
+ // FIXME: refcounting
+ ObjTableEntry *ote = table.lookup(id);
+ if (!ote || !ote->hdr.Valid)
+ return NULL;
+
+ if (ote->hdr.Pointer)
+ return ote->ptr.object;
+
+ return &ote->obj;
+ }
+
+ ObjectHdr *IDSpace::get_local(Object *obj)
+ {
+ if (&obj->aspace->idspace == this)
+ return obj;
+
+ Lock::AutoSpinLock autolock(lock);
+ ObjectPtr *ptr = *rmap.find(obj);
+ if (ptr)
+ return ptr;
+
+ int id = alloc.alloc();
+ ptr = &table.lookup(id, true)->ptr;
+
+ ptr->id = id;
+ ptr->flags = 0;
+ ptr->Valid = 1;
+ ptr->Pointer = 1;
+ ptr->object = obj;
+
+ rmap.add(obj, ptr);
+ return ptr;
+ }
+
+ Object *IDSpace::newobj(Mem::ProcAddrSpace *aspace)
+ {
+ Lock::AutoSpinLock autolock(lock);
+ int id = alloc.alloc();
+ Object *obj = &table.lookup(id, true)->obj;
+
+ obj->id = id;
+ obj->flags = 0;
+ obj->Valid = 1;
+ obj->aspace = aspace;
+
+ return obj;
+ }
+
+ static void copy_data(u8 *dest, u8 *vaddr, size_t bufsize,
+ ParamInfoBlock *dpib, ParamInfoBlock *spib,
+ u32 fmask, u32 flags)
+ {
+ size_t copied = 0;
+
+ for (uint i = 0; i < spib->num_segments; i++) {
+ if ((spib->segments[i].flags & fmask) != flags)
+ continue;
+
+ size_t len = round_up(spib->segments[i].len, 3);
+
+ if (len + copied > bufsize || len + copied < copied)
+ throw_idl(InvalidArgument, 0, countarray("copy_data: bad size"));
+
+ dpib->segments[i].ptr = vaddr;
+ dpib->segments[i].len = len;
+ dpib->segments[i].reserved = 0;
+ Arch::copyin(spib->segments[i].ptr, dest, len);
-extern "C" void invoke_method(ulong objid, ulong methid,
- ParamInfoBlock *user_pib, ulong ret_pc)
-{
- ParamInfoBlock pib = Arch::copyin(user_pib);
- CallFrame *frame = new_frame(curthread);
+ copied += len;
+ dest += len;
+ vaddr += len;
+ }
+ }
+
+ typedef void (*KernEntry)(ParamInfoBlock *pib);
+
+ struct KernObject : public Object {
+ KernEntry entry;
+ };
+
+ typedef Util::RadixTree<KernObject, ID, 6> KIDTable;
+ KIDTable *kernobjs;
- frame->object = objid;
- frame->method = methid;
- frame->caller_user_pib = user_pib;
- frame->ret_pc = ret_pc;
+ void copy_ids_to_kern(ParamInfoBlock *dpib, ParamInfoBlock *spib,
+ IDSpace *ids)
+ {
+ dpib->objlist_len = spib->objlist_len;
- printf("invoke_method: frame %p object %lx method %lx pib %p ret %lx\n",
- frame, frame->object, frame->method, frame->caller_user_pib,
- frame->ret_pc);
+ for (uint i = 0; i < spib->objlist_len; i++) {
+ Object *obj = ids->lookup(spib->objlist[i]);
+
+ if (!obj)
+ throw_idl(InvalidReference, i, nullarray);
+
+ dpib->objlist[i] = reinterpret_cast<uintptr_t>(obj);
+ }
+ }
+
+ void invoke_method(ParamInfoBlock *user_pib, uintptr_t &stack)
+ {
+ printf("invoke_method: pib %p\n", user_pib);
+ ParamInfoBlock pib = Arch::copyin(user_pib);
+ printf("objlist len %lu\n", pib.objlist_len);
+
+ if (pib.objlist_len == 0)
+ throw_idl(InvalidArgument, 0, countarray("no objects"));
+
+ // FIXME: declare constants somewhere
+ if (pib.num_segments > 64)
+ throw_idl(InvalidArgument, 0, countarray("too many segments"));
+ if (pib.objlist_len > 4096)
+ throw_idl(InvalidArgument, 0, countarray("too many objects"));
+
+ printf("&pib.objlist[0] %p\n", &pib.objlist[0]);
+
+ IDSpace *ids = &curthread->aspace->idspace;
+ ID objid = Arch::copyin(&pib.objlist[0]);
+ Object *obj = ids->lookup(objid);
+
+ if (!obj) {
+ printf("no obj %d\n", objid);
+ throw_idl(InvalidReference, 0, nullarray);
+ }
+
+ printf("obj %p\n", obj);
+
+ if (obj->aspace == Arch::init_thread->aspace) {
+ KernObject *ko = static_cast<KernObject *>(obj);
+ int datalen = round_up(pib.buffer_size, 8);
+ int buflen = datalen + pib.objlist_len * sizeof(void *);
+ int piboff = datalen;
+ buflen += sizeof(ParamInfoBlock);
+ buflen += pib.num_segments * sizeof(ParamInfoBlock::Segment);
+
+ u8 *args = new(orbmm) u8[buflen];
+ u8 *copy = new(orbmm) u8[pib.copy_size];
+ ParamInfoBlock *dpib = reinterpret_cast<ParamInfoBlock *>
+ (args + piboff);
+ dpib->objlist = reinterpret_cast<uintptr_t *>(args + datalen);
+
+ copy_ids_to_kern(dpib, &pib, ids);
+ copy_data(args, args, pib.buffer_size, dpib, &pib, ~0U, Segment::In);
+ copy_data(copy, copy, pib.copy_size, dpib, &pib, ~0U,
+ Segment::In | Segment::Copy);
+
+ ko->entry(dpib);
+
+ // FIXME: Copy return data
+ return;
+ }
+
+ CallFrame *frame = new_frame(curthread);
+ frame->caller_user_pib = user_pib;
+ frame->ret_stack = stack;
+
+ printf("invoke_method: frame %p pib %p ret_stack %lx obj %p\n",
+ frame, frame->caller_user_pib,
+ frame->ret_stack, obj);
+ }
+
+ uintptr_t return_from_method(uintptr_t &exptr, size_t &exlen)
+ {
+ return 0;
+ }
+ void init()
+ {
+ kernobjs = new KIDTable;
+ }
}