[kernel] Add capabilities to handles

This change finally adds capabilities to handles. Included changes:

- j6_handle_t is now again 64 bits, with the highest 8 bits being a type
  code, and the next highest 24 bits being the capability mask, so that
  programs can check type/caps without calling the kernel.
- The definitions grammar now includes a `capabilities [ ]` section on
  objects, to list what capabilities are relevant.
- j6/caps.h is auto-generated from object capability lists
- init_libj6 again sets __handle_self and __handle_sys, this is a bit
  of a hack.
- A new syscall, j6_handle_list, will return the list of existing
  handles owned by the calling process.
- syscall_verify.cpp.cog now actually checks that the needed
  capabilities exist on handles before allowing the call.
This commit is contained in:
Justin C. Miller
2022-01-28 01:49:26 -08:00
parent 9b75acf0b5
commit f1246f84e0
38 changed files with 290 additions and 177 deletions

View File

@@ -68,6 +68,14 @@ class Context:
from .types import ObjectRef
ObjectRef.connect(objects)
for obj in objects.values():
for method in obj.methods:
caps = method.options.get("cap", list())
for cap in caps:
if not cap in obj.caps:
from .errors import UnknownCapError
raise UnknownCapError(f"Unknown capability {cap} on {obj.name}::{method.name}")
self.objects.update(objects)
self.interfaces.update(interfaces)

View File

@@ -1,2 +1,3 @@
class InvalidType(Exception): pass
class UnknownTypeError(Exception): pass
class UnknownCapError(Exception): pass

View File

@@ -1,62 +0,0 @@
def generate_template(template, outfile, **kwargs):
from hashlib import md5
from os import makedirs
from os.path import dirname, exists
content = template.render(**kwargs)
h = md5(content.encode('utf-8')).hexdigest()
if exists(outfile):
existing = open(outfile, 'r').read().encode('utf-8')
if md5(existing).hexdigest() == h:
return False
makedirs(dirname(outfile), exist_ok=True)
open(outfile, 'w').write(content)
return True
def generate(ctx, outdir, template_type):
from os.path import basename, join, split, splitext
from jinja2 import Environment, PackageLoader
for name, interface in ctx.interfaces.items():
base = "_".join(sorted(interface.options))
path = join("templates", base, template_type)
env = Environment(
loader = PackageLoader('definitions', package_path=path),
trim_blocks = True, lstrip_blocks = True)
env.filters
for template_name in env.list_templates():
template = env.get_template(template_name)
basepath, filename = split(template_name)
filename, ext = splitext(filename)
if filename == "_object_":
for obj in ctx.objects.values():
outfile = join(outdir, basepath, obj.name + ext)
wrote = generate_template(
template, outfile,
filename=obj.name + ext,
basepath=basepath,
object=obj,
interface=interface,
objects=ctx.objects)
if wrote and ctx.verbose:
print(f"Writing {outfile}")
else:
outfile = join(outdir, template_name)
wrote = generate_template(
template, outfile,
filename=basename(template_name),
interface=interface,
objects=ctx.objects)
if wrote and ctx.verbose:
print(f"Writing {outfile}")

File diff suppressed because one or more lines are too long

View File

@@ -1,12 +1,13 @@
from .parser import Transformer, v_args
def get_opts(args):
from .types import CName, Description, Options, Type, UID
from .types import Caps, CName, Description, Options, Type, UID
kinds = {
Description: "desc",
Options: "opts",
CName: "cname",
Caps: "caps",
UID: "uid",
Type: "typename",
}
@@ -112,6 +113,10 @@ class DefTransformer(Transformer):
from .types import Options
return Options([str(s) for s in args])
def capabilities(self, args):
from .types import Caps
return Caps([str(s) for s in args])
def description(self, s):
from .types import Description
return Description("\n".join(s))
@@ -140,6 +145,9 @@ class DefTransformer(Transformer):
def COMMENT(self, s):
return s[2:].strip()
def OPTION(self, s):
return str(s)
def IDENTIFIER(self, s):
return str(s)

View File

@@ -5,16 +5,13 @@ def _indent(x):
class CName(str): pass
class Description(str): pass
class Import(str): pass
class Caps(list): pass
class Options(dict):
def __init__(self, opts = tuple()):
for opt in opts:
parts = opt.split(":", 1)
self[parts[0]] = "".join(parts[1:])
def __str__(self):
if not self: return ""
return "[{}]".format(" ".join(self.keys()))
self[parts[0]] = self.get(parts[0], []) + ["".join(parts[1:])]
class UID(int):
def __str__(self):

View File

@@ -43,6 +43,11 @@ class Param:
self.options = opts
self.desc = desc
self.caps = set()
for key, values in opts.items():
if key == "cap":
self.caps.update(values)
def __str__(self):
return "param {} {} {} {}".format(
self.name, repr(self.type), self.options, self.desc or "")

View File

@@ -1,8 +1,8 @@
from . import _indent
from . import Options
from . import Caps, Options
class Object:
def __init__(self, name, uid, typename=None, opts=Options(), desc="", children=tuple(), cname=None):
def __init__(self, name, uid, typename=None, opts=Options(), caps=Caps(), desc="", children=tuple(), cname=None):
self.name = name
self.uid = uid
self.options = opts
@@ -11,6 +11,8 @@ class Object:
self.methods = children
self.cname = cname or name
self.caps = set(caps)
from . import ObjectRef
self.__ref = ObjectRef(name)