Review and link kernel_module/user examples to README

This commit is contained in:
Ciro Santilli
2018-07-09 10:58:18 +01:00
parent 258add9c9d
commit f2bf644cfd
15 changed files with 121 additions and 74 deletions

View File

@@ -1,3 +1,5 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#anonymous-inode */
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>

View File

@@ -3,13 +3,17 @@
#define _XOPEN_SOURCE 700
#include <fcntl.h> /* open */
#include <math.h> /* fabs */
#include <stdint.h> /* uint64_t */
#include <stdlib.h> /* size_t */
#include <stdio.h> /* snprintf */
#include <sys/types.h>
#include <unistd.h> /* pread, sysconf */
#include <stdbool.h>
/* Format documented at:
* https://github.com/torvalds/linux/blob/v4.9/Documentation/vm/pagemap.txt
**/
*/
typedef struct {
uint64_t pfn : 54;
unsigned int soft_dirty : 1;
@@ -33,8 +37,12 @@ int pagemap_get_entry(PagemapEntry *entry, int pagemap_fd, uintptr_t vaddr)
nread = 0;
while (nread < sizeof(data)) {
ret = pread(pagemap_fd, &data, sizeof(data),
(vaddr / sysconf(_SC_PAGE_SIZE)) * sizeof(data) + nread);
ret = pread(
pagemap_fd,
&data,
sizeof(data),
(vaddr / sysconf(_SC_PAGE_SIZE)) * sizeof(data) + nread
);
nread += ret;
if (ret <= 0) {
return 1;
@@ -74,4 +82,18 @@ int virt_to_phys_user(uintptr_t *paddr, pid_t pid, uintptr_t vaddr)
return 0;
}
bool common_vector_equal(size_t n, double * v1, double * v2, double max_err)
{
double sum = 0.0;
double diff;
size_t i;
for (i = 0; i < n; ++i) {
diff = v1[i] - v2[i];
sum += diff * diff;
}
if (sqrt(sum)/n > max_err)
return false;
return true;
}
#endif

View File

@@ -1,3 +1,5 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#ctrl-alt-del */
#define _XOPEN_SOURCE 700
#include <signal.h>
#include <stdio.h>

View File

@@ -1,4 +1,6 @@
/* Adapted from: https://eigen.tuxfamily.org/dox/GettingStarted.html */
/* https://github.com/cirosantilli/linux-kernel-module-cheat#eigen
* Adapted from: https://eigen.tuxfamily.org/dox/GettingStarted.html
*/
#include <iostream>
#include <Eigen/Dense>
int main() {

View File

@@ -1,24 +0,0 @@
/* Adapted from: https://eigen.tuxfamily.org/dox/classEigen_1_1JacobiSVD.html */
#include <iostream>
using std::cout;
using std::endl;
#include <Eigen/Core>
#include <Eigen/SVD>
using Eigen::ComputeThinU;
using Eigen::ComputeThinV;
using Eigen::JacobiSVD;
using Eigen::MatrixXf;
using Eigen::Vector3f;
int main() {
MatrixXf m = MatrixXf::Random(3,2);
JacobiSVD<MatrixXf> svd(m, ComputeThinU | ComputeThinV);
Vector3f rhs(1, 0, 0);
cout << "m = " << endl << m << endl << endl;
cout << "svd.singularValues() = " << endl << svd.singularValues() << endl << endl;
cout << "svd.MatrixU() = " << endl << svd.matrixU() << endl << endl;
cout << "svd.MatrixV() = " << endl << svd.matrixV() << endl << endl;
cout << "svd.solve() = " << endl << svd.solve(rhs) << endl << endl;
}

View File

@@ -1,4 +1,5 @@
/* Adapted from: https://github.com/dvdhrm/docs/blob/fad7c3203b14e67053e0fc41d8490138b8ff47dd/drm-howto/modeset.c */
/* https://github.com/cirosantilli/linux-kernel-module-cheat#drm
* Adapted from: https://github.com/dvdhrm/docs/blob/fad7c3203b14e67053e0fc41d8490138b8ff47dd/drm-howto/modeset.c */
/*
* modeset - DRM Modesetting Example

View File

@@ -1,3 +1,5 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#m5ops-instructions */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

View File

@@ -1,3 +1,5 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#mmap */
#define _XOPEN_SOURCE 700
#include <assert.h>
#include <fcntl.h>

View File

@@ -1,4 +1,4 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kernel-module-parameters#myinsmod */
/* https://github.com/cirosantilli/linux-kernel-module-cheat#myinsmod */
#define _GNU_SOURCE
#include <fcntl.h>

View File

@@ -1,3 +1,5 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#myinsmod */
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>

View File

@@ -1,15 +1,16 @@
/* Adapted from: https://github.com/xianyi/OpenBLAS/wiki/User-Manual/59b62f98e7400270fb03ad1d85fba5b64ebbff2b#call-cblas-interface */
/* https://github.com/cirosantilli/linux-kernel-module-cheat#blas
* Adapted from: https://github.com/xianyi/OpenBLAS/wiki/User-Manual/59b62f98e7400270fb03ad1d85fba5b64ebbff2b#call-cblas-interface */
#include "common.h"
#include <assert.h>
#include <cblas.h>
#include <stdio.h>
int main() {
size_t i = 0;
double A[6] = {1.0,2.0,1.0,-3.0,4.0,-1.0};
double B[6] = {1.0,2.0,1.0,-3.0,4.0,-1.0};
double C[9] = {.5,.5,.5,.5,.5,.5,.5,.5,.5};
cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans,3,3,2,1,A, 3, B, 3,2,C,3);
for(i = 0; i < 9; i++)
printf("%f ", C[i]);
printf("\n");
int main()
{
double A[6] = {1.0, 2.0, 1.0, -3.0, 4.0, -1.0};
double B[6] = {1.0, 2.0, 1.0, -3.0, 4.0, -1.0};
double C[9] = {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5};
cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans, 3, 3, 2, 1, A, 3, B, 3, 2, C, 3);
assert(common_vector_equal(9, C, (double[]){11.0, -9.0, 5.0, -9.0, 21.0, -1.0, 5.0, -1.0, 3.0}, 1e-6));
}

View File

@@ -1,3 +1,5 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#openmp */
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

View File

@@ -1,3 +1,5 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#poll */
#define _XOPEN_SOURCE 700
#include <assert.h>
#include <fcntl.h> /* creat, O_CREAT */

View File

@@ -1,9 +1,4 @@
/*
Only works in x86_64.
- https://en.wikipedia.org/wiki/Time_Stamp_Counter
- https://stackoverflow.com/questions/9887839/clock-cycle-count-wth-gcc/9887979
*/
/* https://github.com/cirosantilli/linux-kernel-module-cheat#rdtsc */
#include <stdint.h>
#include <stdio.h>