-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BitMap : add a simple example for API test
Change-Id: I2dac5e59375f6512bf213e88695f202786781cf1
- Loading branch information
Showing
4 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
*.cmd | ||
*.o | ||
*.mod.c | ||
*.ko | ||
*.order | ||
Module.symvers | ||
.tmp_versions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Makefile | ||
# Comment/uncomment the following line to disable/enable debugging | ||
# DEBUG = y | ||
|
||
# Usage | ||
# make CROSS_COMPILE=<cross_compiler_prefix> KERNEL_DIR=<your_kernel_dir> KERNEL_BUID_OUTPUT=<kernel_buid_output> | ||
# | ||
# make CROSS_COMPILE=/home/zeroway/rk3399/tool/gcc-linaro-4.9.4-2017.01-i686_aarch64-linux-gnu/bin/aarch64-linux-gnu- KERNEL_DIR=/home/zeroway/rk3399/src/firefly/kernel KERNEL_BUID_OUTPUT=/home/zeroway/rk3399/src/firefly/out/target/product/rk3399_firefly_box/obj/KERNEL | ||
|
||
# Add your debugging flag (or not) to CFLAGS | ||
ifeq ($(DEBUG),y) | ||
DEBFLAGS = -O -g -DSCULL_DEBUG # "-O" is needed to expand inlines | ||
else | ||
DEBFLAGS = -O2 | ||
endif | ||
|
||
obj-m := bitmap_test.o | ||
|
||
KERNEL_DIR ?= /lib/modules/`uname -r`/build | ||
KERNEL_BUID_OUTPUT ?=$(KERNEL_DIR) | ||
CC = $(CROSS_COMPILE)gcc | ||
LD = $(CROSS_COMPILE)ld | ||
PWD := $(shell pwd) | ||
ARCH := x86_64 | ||
|
||
modules: | ||
$(MAKE) -C $(KERNEL_DIR) ARCH=$(ARCH) M=$(PWD) O=$(KERNEL_DIR) modules | ||
|
||
clean: | ||
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions modules.order Module.symvers | ||
|
||
depend .depend dep: | ||
$(CC) $(CFLAGS) -M *.c > .depend | ||
|
||
ifeq (.depend,$(wildcard .depend)) | ||
include .depend | ||
endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include <linux/init.h> | ||
#include <linux/module.h> | ||
|
||
/* https://www.cnblogs.com/schips/p/10674687.html */ | ||
|
||
#define BITMAP_LEN 10 | ||
|
||
#define USING_UNLONG | ||
#ifdef USING_UNLONG | ||
unsigned long mybm[1]; | ||
#else | ||
DECLARE_BITMAP(mybm, BITMAP_LEN); | ||
#endif | ||
|
||
static int bitmap_test_init(void) | ||
{ | ||
int bit; | ||
|
||
/* architecture specifiy API */ | ||
/* | ||
* non atomic version | ||
* include/asm-generic/bitops/instrumented-non-atomic.h | ||
*/ | ||
__set_bit(1, mybm); | ||
/* | ||
* arch___set_bit(nr, addr); | ||
* asm volatile(__ASM_SIZE(bts) " %1,%0" : : ADDR, "Ir" (nr) : "memory"); | ||
*/ | ||
if (!test_bit(1, mybm)) | ||
printk(KERN_ALERT" bit 1 tested not set\n"); | ||
else | ||
printk(KERN_ALERT" bit 1 tested set\n"); | ||
|
||
__clear_bit(1, mybm); | ||
if (!test_bit(1, mybm)) | ||
printk(KERN_ALERT" bit 1 tested not set\n"); | ||
else | ||
printk(KERN_ALERT" bit 1 tested set\n"); | ||
|
||
/* | ||
* atomic version | ||
* include/asm-generic/bitops/atomic.h | ||
* arch/x86/boot/bitops.h | ||
*/ | ||
set_bit(2, mybm); | ||
if (!test_bit(2, mybm)) | ||
printk(KERN_ALERT" bit 2 tested not set\n"); | ||
else | ||
printk(KERN_ALERT" bit 2 tested set\n"); | ||
|
||
clear_bit(2, mybm); | ||
if (!test_bit(2, mybm)) | ||
printk(KERN_ALERT" bit 2 tested not set\n"); | ||
else | ||
printk(KERN_ALERT" bit 2 tested set\n"); | ||
|
||
/* Generic API */ | ||
bitmap_zero(mybm, BITMAP_LEN); | ||
if (!test_bit(3, mybm)) | ||
printk(KERN_ALERT" bit 3 tested not set\n"); | ||
else | ||
printk(KERN_ALERT" bit 3 tested set\n"); | ||
|
||
bitmap_fill(mybm, BITMAP_LEN); | ||
if (!test_bit(3, mybm)) | ||
printk(KERN_ALERT" bit 3 tested not set\n"); | ||
else | ||
printk(KERN_ALERT" bit 3 tested set\n"); | ||
|
||
/* clear all before test */ | ||
bitmap_zero(mybm, BITMAP_LEN); | ||
set_bit(4, mybm); | ||
set_bit(8, mybm); | ||
set_bit(9, mybm); | ||
/* iterator all the setted bit */ | ||
for_each_set_bit(bit, mybm, BITMAP_LEN) | ||
printk(KERN_ALERT" %d\n", bit); | ||
printk(KERN_ALERT"===========\n"); | ||
|
||
/* iterator all the no setted bit */ | ||
for_each_clear_bit(bit, mybm, BITMAP_LEN) | ||
printk(KERN_ALERT" %d\n", bit); | ||
printk(KERN_ALERT"===========\n"); | ||
|
||
printk(KERN_ALERT"BitMap test Done\n"); | ||
|
||
return 0; | ||
} | ||
|
||
static void bitmap_test_exit(void) | ||
{ | ||
printk(KERN_ALERT"BitMap test Exit\n"); | ||
} | ||
|
||
MODULE_LICENSE("Dual BSD/GPL"); | ||
module_init(bitmap_test_init); | ||
module_exit(bitmap_test_exit); |