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

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