"task_struct", (
"struct task_struct {", "};", true,
[ "state"; "prio"; "normal_prio"; "static_prio";
- "tasks'prev"; "tasks'next"; "comm"]
+ "tasks'prev"; "tasks'next"; "mm"; "active_mm"; "comm"]
);
(*
"mm_struct", (
);
]
-let debug = true
+let debug = false
open Camlp4.PreCast
open Syntax
let fields = List.map (
function
| (name, `Int) ->
- <:ctyp< $lid:name$ : int >>
+ <:ctyp< $lid:name$ : int64 >>
+ | (name, `Ptr "list_head") ->
+ <:ctyp< $lid:name$ :
+ [ `$lid:struct_name$ ] Virt_mem_mmap.typed_addr >>
| (name, `Ptr struct_name) ->
- <:ctyp< $lid:name$ : [`$lid:struct_name$] int64 >>
+ <:ctyp< $lid:name$ :
+ [ `$lid:struct_name$ ] Virt_mem_mmap.typed_addr >>
| (name, `Str _) ->
<:ctyp< $lid:name$ : string >>
) field_types in
struct_type, struct_sig in
(* The shared parser functions.
+ *
* We could include bitmatch statements directly in here, but
* what happens is that the macros get expanded here, resulting
- * in unreadable generated code. So instead just do a textual
- * substitution later by post-processing the generated files.
- * Not type-safe, but we can't have everything.
+ * in (even more) unreadable generated code. So instead just
+ * do a textual substitution later by post-processing the
+ * generated files. Not type-safe, but we can't have
+ * everything.
*)
let parser_stmts, parser_subs =
let parser_stmts = List.map (
let fnname = sprintf "parser_%d" i in
<:str_item<
let $lid:fnname$ bits = $str:fnname$
- >>
+ >>
) parsers in
let parser_stmts =
let parser_subs = List.map (
fun (i, (endian, fields)) ->
let fnname = sprintf "parser_%d" i in
- let patterns = "" and assignments = "" in (* XXX *)
+ let endian =
+ match endian with
+ | Bitstring.LittleEndian -> "littleendian"
+ | Bitstring.BigEndian -> "bigendian"
+ | _ -> assert false in
+ let patterns =
+ (* Fields must be sorted by offset, otherwise bitmatch
+ * will complain.
+ *)
+ let cmp (_, (_, o1, _)) (_, (_, o2, _)) = compare o1 o2 in
+ let fields = List.sort ~cmp fields in
+ String.concat ";\n " (
+ List.map (
+ function
+ | (field_name, (`Int, offset, size))
+ | (field_name, (`Ptr _, offset, size)) ->
+ (* 'zero+' is a hack to force the type to int64. *)
+ sprintf "%s : zero+%d : offset(%d), %s"
+ field_name (size*8) (offset*8) endian
+ | (field_name, (`Str width, offset, size)) ->
+ sprintf "%s : %d : offset(%d), string"
+ field_name (width*8) (offset*8)
+ ) fields
+ ) in
+ let assignments =
+ String.concat ";\n " (
+ List.map (
+ function
+ | (field_name, (`Ptr "list_head", offset, size)) ->
+ sprintf "%s = (Virt_mem_mmap.unsafe_typed_addr_of_addr (Int64.sub %s %dL) : [ `%s ] Virt_mem_mmap.typed_addr)" field_name field_name offset struct_name
+ | (field_name, (`Ptr struct_name, offset, size)) ->
+ sprintf "%s = (Virt_mem_mmap.unsafe_typed_addr_of_addr %s : [ `%s ] Virt_mem_mmap.typed_addr)" field_name field_name struct_name
+ | (field_name, _) ->
+ sprintf "%s = %s" field_name field_name
+ ) fields
+ ) in
+
let sub =
- sprintf "bitmatch bits with
- | { %s } -> { %s }
- | { _ } -> raise (ParseError (%S, %S, \"failed to match kernel structure\"))"
+ sprintf "\
+ bitmatch bits with
+ | { %s } -> { %s }
+ | { _ } -> raise (ParseError (%S, %S, \"failed to match kernel structure\"))"
patterns assignments struct_name fnname in
+
fnname, sub
) parsers in
(* Code (.ml file). *)
let code = <:str_item<
let warning = "This code is automatically generated from the kernel database by kerneldb-to-parser program. Any edits you make will be lost."
- exception ParseError of string ;;
+ let zero = 0
+ exception ParseError of string * string * string;;
$struct_type$
$parser_stmts$
$version_map$
let size version =
let _, size = StringMap.find version map in
size
- let get version bits =
+ let of_bits version bits =
let parsefn, _ = StringMap.find version map in
parsefn bits
+ let get version mem addr =
+ let parsefn, size = StringMap.find version map in
+ let addr = Virt_mem_mmap.unsafe_addr_of_typed_addr addr in
+ let bytes = Virt_mem_mmap.get_bytes mem addr size in
+ let bits = Bitstring.bitstring_of_string bytes in
+ parsefn bits
>> in
(* Interface (.mli file). *)
let interface = <:sig_item<
- exception ParseError of string ;;
+ exception ParseError of string * string * string;;
$struct_sig$
type kernel_version = string
val known : kernel_version -> bool
val size : kernel_version -> int
- val get : kernel_version -> Bitstring.bitstring -> t
+ val of_bits : kernel_version -> Bitstring.bitstring -> t
+ val get : kernel_version ->
+ ('a, 'b, [`HasMapping]) Virt_mem_mmap.t ->
+ [ `$lid:struct_name$ ] Virt_mem_mmap.typed_addr ->
+ t
>> in
- (struct_name, code, interface)
+ (struct_name, code, interface, parser_subs)
) files in
(* Finally generate the output files. *)
+ let re_subst = Pcre.regexp "^(.*)\"(parser_\\d+)\"(.*)$" in
+
List.iter (
- fun (struct_name, code, interface) ->
+ fun (struct_name, code, interface, parser_subs) ->
+ (* Interface (.mli file). *)
+ let output_file = outputdir // "kernel_" ^ struct_name ^ ".mli" in
+ printf "Writing %s interface to %s ...\n%!" struct_name output_file;
+ Printers.OCaml.print_interf ~output_file interface;
+
+ (* Implementation (.ml file). *)
let output_file = outputdir // "kernel_" ^ struct_name ^ ".ml" in
printf "Writing %s implementation to %s ...\n%!" struct_name output_file;
- Printers.OCaml.print_implem ~output_file code;
- let output_file = outputdir // "kernel_" ^ struct_name ^ ".mli" in
- printf "Writing %s interface to %s ...\n%!" struct_name output_file;
- Printers.OCaml.print_interf ~output_file interface
+ let new_output_file = output_file ^ ".new" in
+ Printers.OCaml.print_implem ~output_file:new_output_file code;
+
+ (* Substitute the parser bodies in the output file. *)
+ let ichan = open_in new_output_file in
+ let ochan = open_out output_file in
+
+ let rec loop () =
+ let line = input_line ichan in
+ let line =
+ if Pcre.pmatch ~rex:re_subst line then (
+ let subs = Pcre.exec ~rex:re_subst line in
+ let start = Pcre.get_substring subs 1 in
+ let template = Pcre.get_substring subs 2 in
+ let rest = Pcre.get_substring subs 3 in
+ let sub = List.assoc template parser_subs in
+ start ^ sub ^ rest
+ ) else line in
+ output_string ochan line; output_char ochan '\n';
+ loop ()
+ in
+ (try loop () with End_of_file -> ());
+
+ close_out ochan;
+ close_in ichan;
+
+ Unix.unlink new_output_file
) files