Blame SOURCES/rhbz1643997.0006-stapbpf-assembler-WIP-5-basic-kernel_string-implemen.patch

132810
From f614583a605f4c23d2f6e1a2ad31e089d10f8d8e Mon Sep 17 00:00:00 2001
132810
From: Serhei Makarov <smakarov@redhat.com>
132810
Date: Tue, 16 Oct 2018 18:17:25 -0400
132810
Subject: [PATCH 06/32] stapbpf assembler WIP #5 :: basic kernel_string()
132810
 implementation
132810
132810
---
132810
 tapset/bpf/conversions.stp | 108 +++++++++++++++++++++++++++++++++++++++++++++
132810
 1 file changed, 108 insertions(+)
132810
 create mode 100644 tapset/bpf/conversions.stp
132810
132810
diff --git a/tapset/bpf/conversions.stp b/tapset/bpf/conversions.stp
132810
new file mode 100644
132810
index 000000000..d741ec584
132810
--- /dev/null
132810
+++ b/tapset/bpf/conversions.stp
132810
@@ -0,0 +1,108 @@
132810
+// conversions tapset -- eBPF version
132810
+// Copyright (C) 2018 Red Hat Inc.
132810
+//
132810
+// This file is part of systemtap, and is free software.  You can
132810
+// redistribute it and/or modify it under the terms of the GNU General
132810
+// Public License (GPL); either version 2, or (at your option) any
132810
+// later version.
132810
+
132810
+/**
132810
+ * sfunction kernel_string - Retrieves string from kernel memory
132810
+ * @addr: The kernel address to retrieve the string from
132810
+ *
132810
+ * Description: This function returns the null terminated C string
132810
+ * from a given kernel memory address. Reports an error on string
132810
+ * copy fault.
132810
+ */
132810
+function kernel_string:string (addr:long) {
132810
+  return kernel_string_n(addr, 64 /*TODO: define BPF_MAXSTRINGLEN*/)
132810
+}
132810
+
132810
+// TODO kernel_string:string(addr:long, err_msg:string)
132810
+// TODO kernel_string2:string(addr:long, err_msg:string)
132810
+
132810
+/**
132810
+ * sfunction kernel_string - Retrieves string from kernel memory with alternative error string
132810
+ * @addr: The kernel address to retrieve the string from
132810
+ * @err_msg: The error message to return when data isn't available
132810
+ *
132810
+ * Description: This function returns the null terminated C string
132810
+ * from a given kernel memory address. Reports the given error message
132810
+ * on string copy fault.
132810
+ */
132810
+function kernel_string:string (addr:long, err_msg:string)
132810
+%{ /* bpf */ /* pure */
132810
+  /* buf = bpf_stk_alloc(BPF_MAXSTRINGLEN);
132810
+     buf[0] = 0x0; // guarantee NUL byte
132810
+     rc = bpf_probe_read_str(buf, n, addr); */
132810
+  alloc, $buf, BPF_MAXSTRINGLEN;
132810
+  0x62, $buf, -, -, 0x0; /* stw [$buf+0], 0x0 -- guarantee NUL byte */
132810
+  call, $rc, bpf_probe_read_str, $buf, BPF_MAXSTRINGLEN, $addr;
132810
+
132810
+  /* if (rc < 0) return err_msg;
132810
+     return buf; */
132810
+  0xa5, rc, 0, _err, -; /* jlt $rc, 0, _err */
132810
+  0xbf, $$, $buf, -, -; /* mov $$, $buf */
132810
+  0x05, -, -, _done, -; /* ja _done; */
132810
+  label, _err;
132810
+  0xbf, $$, $err_msg, -, -; /* mov $$, $err_msg */
132810
+  label, _done;
132810
+%}
132810
+function kernel_string2:string (addr:long, err_msg:string) {
132810
+  return kernel_string(addr, err_msg);
132810
+}
132810
+
132810
+// TODO kernel_string_quoted:string(addr:long) -- requires pseudo-loop to quote unprintable chars
132810
+
132810
+/**
132810
+ * sfunction kernel_string_n - Retrieves string of given length from kernel memory
132810
+ * @addr: The kernel address to retrieve the string from
132810
+ * @n: The maximum length of the string (if not null terminated)
132810
+ *
132810
+ * Description: Returns the C string of a maximum given length from a
132810
+ * given kernel memory address. Reports an error on string copy fault.
132810
+ */
132810
+function kernel_string_n:string (addr:long, n:long)
132810
+%{ /* bpf */ /* pure */
132810
+  /* if (n > BPF_MAXSTRINGLEN) n = BPF_MAXSTRINGLEN; */
132810
+  0xb5, $n, -, _skip, BPF_MAXSTRINGLEN; /* jle n, BPF_MAXSTRINGLEN, _skip */
132810
+  0xb7, $n, -, -, BPF_MAXSTRINGLEN; /* mov $n, BPF_MAXSTRINGLEN */
132810
+  label, _skip;
132810
+
132810
+  /* buf = bpf_stk_alloc(BPF_MAXSTRINGLEN);
132810
+     buf[0] = 0x0; // guarantee NUL byte
132810
+     rc = bpf_probe_read_str(buf, n, addr); */
132810
+  alloc, $buf, BPF_MAXSTRINGLEN;
132810
+  0x62, $buf, -, -, 0x0; /* stw [buf+0], 0 -- guarantee NUL byte */
132810
+  call, $rc, probe_read_str, $buf, $n, $addr; /* TODO: should work with bpf_probe_read_str too */
132810
+
132810
+  /* TODO pending implementation of error */
132810
+  /* if (rc < 0) error("...", addr); */
132810
+  /*0x35, $rc, 0, _done, -; /* jge rc, 0, _done */
132810
+  /*error, "kernel string copy fault at 0x%p [man error::fault]", $addr; /* TODO document bpf version of error::fault */
132810
+  /*label, _done;*/
132810
+
132810
+  /* return buf; */
132810
+  0xbf, $$, $buf, -, -; /* mov $$, buf */
132810
+%}
132810
+
132810
+// TODO kernel_string_utf32:string(addr:long)
132810
+// TODO kernel_string_utf32:string(addr:long,err_msg:string)
132810
+// TODO kernel_string2_utf32:string(addr:long,err_msg:string)
132810
+// TODO kernel_string_quoted_utf32:string(addr:long)
132810
+
132810
+// TODO kernel_string_utf16:string(addr:long)
132810
+// TODO kernel_string_utf16:string(addr:long,err_msg:string)
132810
+// TODO kernel_string2_utf16:string(addr:long,err_msg:string)
132810
+// TODO kernel_string_quoted_utf16:string(addr:long)
132810
+
132810
+// TODO kernel_long:long(addr:long)
132810
+// TODO kernel_int:long(addr:long)
132810
+// TODO kernel_short:long(addr:long)
132810
+// TODO kernel_char:long(addr:long)
132810
+
132810
+// TODO kernel_pointer:long(addr:long)
132810
+
132810
+// TODO kernel_buffer_quoted:string(addr:long,inlen:long)
132810
+// TODO kernel_buffer_quoted:string(addr:long,inlen:long,outlen:long)
132810
+// TODO kernel_buffer_quoted_error:string(addr:long,inlen:long,outlen:long)
132810
-- 
132810
2.14.5
132810