Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
/*
 * Copyright (c) 2015 Juniper Networks Inc.
 * All rights reserved.
 *
 * Developed by Semihalf.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "opt_ddb.h"

#include <sys/param.h>
#include <sys/types.h>
#include <sys/kdb.h>
#include <sys/pcpu.h>
#include <sys/smp.h>
#include <sys/systm.h>

#include <machine/atomic.h>
#include <machine/armreg.h>
#include <machine/cpu.h>
#include <machine/debug_monitor.h>
#include <machine/kdb.h>
#include <machine/pcb.h>
#include <machine/reg.h>

#include <ddb/ddb.h>
#include <ddb/db_access.h>
#include <ddb/db_sym.h>

enum dbg_t {
	DBG_TYPE_BREAKPOINT = 0,
	DBG_TYPE_WATCHPOINT = 1,
};

struct dbg_wb_conf {
	enum dbg_t		type;
	enum dbg_access_t	access;
	db_addr_t		address;
	db_expr_t		size;
	u_int			slot;
};

static int dbg_reset_state(void);
static int dbg_setup_breakpoint(db_expr_t, db_expr_t, u_int);
static int dbg_remove_breakpoint(u_int);
static u_int dbg_find_slot(enum dbg_t, db_expr_t);
static boolean_t dbg_check_slot_free(enum dbg_t, u_int);

static int dbg_remove_xpoint(struct dbg_wb_conf *);
static int dbg_setup_xpoint(struct dbg_wb_conf *);

static int dbg_capable_var;	/* Indicates that machine is capable of using
				   HW watchpoints/breakpoints */

static uint32_t dbg_model;	/* Debug Arch. Model */
static boolean_t dbg_ossr;	/* OS Save and Restore implemented */

static uint32_t dbg_watchpoint_num;
static uint32_t dbg_breakpoint_num;

/* ID_DFR0 - Debug Feature Register 0 */
#define	ID_DFR0_CP_DEBUG_M_SHIFT	0
#define	ID_DFR0_CP_DEBUG_M_MASK		(0xF << ID_DFR0_CP_DEBUG_M_SHIFT)
#define	ID_DFR0_CP_DEBUG_M_NS		(0x0) /* Not supported */
#define	ID_DFR0_CP_DEBUG_M_V6		(0x2) /* v6 Debug arch. CP14 access */
#define	ID_DFR0_CP_DEBUG_M_V6_1		(0x3) /* v6.1 Debug arch. CP14 access */
#define	ID_DFR0_CP_DEBUG_M_V7		(0x4) /* v7 Debug arch. CP14 access */
#define	ID_DFR0_CP_DEBUG_M_V7_1		(0x5) /* v7.1 Debug arch. CP14 access */

/* DBGDIDR - Debug ID Register */
#define	DBGDIDR_WRPS_SHIFT		28
#define	DBGDIDR_WRPS_MASK		(0xF << DBGDIDR_WRPS_SHIFT)
#define	DBGDIDR_WRPS_NUM(reg)		\
    ((((reg) & DBGDIDR_WRPS_MASK) >> DBGDIDR_WRPS_SHIFT) + 1)

#define	DBGDIDR_BRPS_SHIFT		24
#define	DBGDIDR_BRPS_MASK		(0xF << DBGDIDR_BRPS_SHIFT)
#define	DBGDIDR_BRPS_NUM(reg)		\
    ((((reg) & DBGDIDR_BRPS_MASK) >> DBGDIDR_BRPS_SHIFT) + 1)

/* DBGPRSR - Device Powerdown and Reset Status Register */
#define	DBGPRSR_PU			(1 << 0) /* Powerup status */

/* DBGOSLSR - OS Lock Status Register */
#define	DBGOSLSR_OSLM0			(1 << 0)

/* DBGOSDLR - OS Double Lock Register */
#define	DBGPRSR_DLK			(1 << 0) /* OS Double Lock set */

/* DBGDSCR - Debug Status and Control Register */
#define	DBGSCR_MDBG_EN			(1 << 15) /* Monitor debug-mode enable */

/* DBGWVR - Watchpoint Value Register */
#define	DBGWVR_ADDR_MASK		(~0x3U)

/* Watchpoints/breakpoints control register bitfields */
#define	DBG_WB_CTRL_LEN_1		(0x1 << 5)
#define	DBG_WB_CTRL_LEN_2		(0x3 << 5)
#define	DBG_WB_CTRL_LEN_4		(0xf << 5)
#define	DBG_WB_CTRL_LEN_8		(0xff << 5)
#define	DBG_WB_CTRL_LEN_MASK(x)	((x) & (0xff << 5))
#define	DBG_WB_CTRL_EXEC		(0x0 << 3)
#define	DBG_WB_CTRL_LOAD		(0x1 << 3)
#define	DBG_WB_CTRL_STORE		(0x2 << 3)
#define	DBG_WB_CTRL_ACCESS_MASK(x)	((x) & (0x3 << 3))

/* Common for breakpoint and watchpoint */
#define	DBG_WB_CTRL_PL1		(0x1 << 1)
#define	DBG_WB_CTRL_PL0		(0x2 << 1)
#define	DBG_WB_CTRL_PLX_MASK(x)	((x) & (0x3 << 1))
#define	DBG_WB_CTRL_E		(0x1 << 0)

/*
 * Watchpoint/breakpoint helpers
 */
#define	DBG_BKPT_BT_SLOT	0	/* Slot for branch taken */
#define	DBG_BKPT_BNT_SLOT	1	/* Slot for branch not taken */

#define	OP2_SHIFT		4

/* Opc2 numbers for coprocessor instructions */
#define	DBG_WB_BVR	4
#define	DBG_WB_BCR	5
#define	DBG_WB_WVR	6
#define	DBG_WB_WCR	7

#define	DBG_REG_BASE_BVR	(DBG_WB_BVR << OP2_SHIFT)
#define	DBG_REG_BASE_BCR	(DBG_WB_BCR << OP2_SHIFT)
#define	DBG_REG_BASE_WVR	(DBG_WB_WVR << OP2_SHIFT)
#define	DBG_REG_BASE_WCR	(DBG_WB_WCR << OP2_SHIFT)

#define	DBG_WB_READ(cn, cm, op2, val) do {					\
	__asm __volatile("mrc p14, 0, %0, " #cn "," #cm "," #op2 : "=r" (val));	\
} while (0)

#define	DBG_WB_WRITE(cn, cm, op2, val) do {					\
	__asm __volatile("mcr p14, 0, %0, " #cn "," #cm "," #op2 :: "r" (val));	\
} while (0)

#define	READ_WB_REG_CASE(op2, m, val)			\
	case (((op2) << OP2_SHIFT) + m):		\
		DBG_WB_READ(c0, c ## m, op2, val);	\
		break

#define	WRITE_WB_REG_CASE(op2, m, val)			\
	case (((op2) << OP2_SHIFT) + m):		\
		DBG_WB_WRITE(c0, c ## m, op2, val);	\
		break

#define	SWITCH_CASES_READ_WB_REG(op2, val)	\
	READ_WB_REG_CASE(op2,  0, val);		\
	READ_WB_REG_CASE(op2,  1, val);		\
	READ_WB_REG_CASE(op2,  2, val);		\
	READ_WB_REG_CASE(op2,  3, val);		\
	READ_WB_REG_CASE(op2,  4, val);		\
	READ_WB_REG_CASE(op2,  5, val);		\
	READ_WB_REG_CASE(op2,  6, val);		\
	READ_WB_REG_CASE(op2,  7, val);		\
	READ_WB_REG_CASE(op2,  8, val);		\
	READ_WB_REG_CASE(op2,  9, val);		\
	READ_WB_REG_CASE(op2, 10, val);		\
	READ_WB_REG_CASE(op2, 11, val);		\
	READ_WB_REG_CASE(op2, 12, val);		\
	READ_WB_REG_CASE(op2, 13, val);		\
	READ_WB_REG_CASE(op2, 14, val);		\
	READ_WB_REG_CASE(op2, 15, val)

#define	SWITCH_CASES_WRITE_WB_REG(op2, val)	\
	WRITE_WB_REG_CASE(op2,  0, val);	\
	WRITE_WB_REG_CASE(op2,  1, val);	\
	WRITE_WB_REG_CASE(op2,  2, val);	\
	WRITE_WB_REG_CASE(op2,  3, val);	\
	WRITE_WB_REG_CASE(op2,  4, val);	\
	WRITE_WB_REG_CASE(op2,  5, val);	\
	WRITE_WB_REG_CASE(op2,  6, val);	\
	WRITE_WB_REG_CASE(op2,  7, val);	\
	WRITE_WB_REG_CASE(op2,  8, val);	\
	WRITE_WB_REG_CASE(op2,  9, val);	\
	WRITE_WB_REG_CASE(op2, 10, val);	\
	WRITE_WB_REG_CASE(op2, 11, val);	\
	WRITE_WB_REG_CASE(op2, 12, val);	\
	WRITE_WB_REG_CASE(op2, 13, val);	\
	WRITE_WB_REG_CASE(op2, 14, val);	\
	WRITE_WB_REG_CASE(op2, 15, val)

static uint32_t
dbg_wb_read_reg(int reg, int n)
{
	uint32_t val;

	val = 0;

	switch (reg + n) {
	SWITCH_CASES_READ_WB_REG(DBG_WB_WVR, val);
	SWITCH_CASES_READ_WB_REG(DBG_WB_WCR, val);
	SWITCH_CASES_READ_WB_REG(DBG_WB_BVR, val);
	SWITCH_CASES_READ_WB_REG(DBG_WB_BCR, val);
	default:
		db_printf(
		    "trying to read from CP14 reg. using wrong opc2 %d\n",
		    reg >> OP2_SHIFT);
	}

	return (val);
}

static void
dbg_wb_write_reg(int reg, int n, uint32_t val)
{

	switch (reg + n) {
	SWITCH_CASES_WRITE_WB_REG(DBG_WB_WVR, val);
	SWITCH_CASES_WRITE_WB_REG(DBG_WB_WCR, val);
	SWITCH_CASES_WRITE_WB_REG(DBG_WB_BVR, val);
	SWITCH_CASES_WRITE_WB_REG(DBG_WB_BCR, val);
	default:
		db_printf(
		    "trying to write to CP14 reg. using wrong opc2 %d\n",
		    reg >> OP2_SHIFT);
	}
	isb();
}

static __inline boolean_t
dbg_capable(void)
{

	return (atomic_cmpset_int(&dbg_capable_var, 0, 0) == 0);
}

boolean_t
kdb_cpu_pc_is_singlestep(db_addr_t pc)
{
	/*
	 * XXX: If the platform fails to enable its debug arch.
	 *      there will be no stepping capabilities
	 *      (SOFTWARE_SSTEP is not defined for __ARM_ARCH >= 6).
	 */
	if (!dbg_capable())
		return (FALSE);

	if (dbg_find_slot(DBG_TYPE_BREAKPOINT, pc) != ~0U)
		return (TRUE);

	return (FALSE);
}

void
kdb_cpu_set_singlestep(void)
{
	db_expr_t inst;
	db_addr_t pc, brpc;
	uint32_t wcr;
	u_int i;

	if (!dbg_capable())
		return;

	/*
	 * Disable watchpoints, e.g. stepping over watched instruction will
	 * trigger break exception instead of single-step exception and locks
	 * CPU on that instruction for ever.
	 */
	for (i = 0; i < dbg_watchpoint_num; i++) {
		wcr = dbg_wb_read_reg(DBG_REG_BASE_WCR, i);
		if ((wcr & DBG_WB_CTRL_E) != 0) {
			dbg_wb_write_reg(DBG_REG_BASE_WCR, i,
			    (wcr & ~DBG_WB_CTRL_E));
		}
	}

	pc = PC_REGS();

	inst = db_get_value(pc, sizeof(pc), FALSE);
	if (inst_branch(inst) || inst_call(inst) || inst_return(inst)) {
		brpc = branch_taken(inst, pc);
		dbg_setup_breakpoint(brpc, INSN_SIZE, DBG_BKPT_BT_SLOT);
	}
	pc = next_instr_address(pc, 0);
	dbg_setup_breakpoint(pc, INSN_SIZE, DBG_BKPT_BNT_SLOT);
}

void
kdb_cpu_clear_singlestep(void)
{
	uint32_t wvr, wcr;
	u_int i;

	if (!dbg_capable())
		return;

	dbg_remove_breakpoint(DBG_BKPT_BT_SLOT);
	dbg_remove_breakpoint(DBG_BKPT_BNT_SLOT);

	/* Restore all watchpoints */
	for (i = 0; i < dbg_watchpoint_num; i++) {
		wcr = dbg_wb_read_reg(DBG_REG_BASE_WCR, i);
		wvr = dbg_wb_read_reg(DBG_REG_BASE_WVR, i);
		/* Watchpoint considered not empty if address value is not 0 */
		if ((wvr & DBGWVR_ADDR_MASK) != 0) {
			dbg_wb_write_reg(DBG_REG_BASE_WCR, i,
			    (wcr | DBG_WB_CTRL_E));
		}
	}
}

int
dbg_setup_watchpoint(db_expr_t addr, db_expr_t size, enum dbg_access_t access)
{
	struct dbg_wb_conf conf;

	if (access == HW_BREAKPOINT_X) {
		db_printf("Invalid access type for watchpoint: %d\n", access);
		return (EINVAL);
	}

	conf.address = addr;
	conf.size = size;
	conf.access = access;
	conf.type = DBG_TYPE_WATCHPOINT;

	return (dbg_setup_xpoint(&conf));
}

int
dbg_remove_watchpoint(db_expr_t addr, db_expr_t size __unused)
{
	struct dbg_wb_conf conf;

	conf.address = addr;
	conf.type = DBG_TYPE_WATCHPOINT;

	return (dbg_remove_xpoint(&conf));
}

static int
dbg_setup_breakpoint(db_expr_t addr, db_expr_t size, u_int slot)
{
	struct dbg_wb_conf conf;

	conf.address = addr;
	conf.size = size;
	conf.access = HW_BREAKPOINT_X;
	conf.type = DBG_TYPE_BREAKPOINT;
	conf.slot = slot;

	return (dbg_setup_xpoint(&conf));
}

static int
dbg_remove_breakpoint(u_int slot)
{
	struct dbg_wb_conf conf;

	/* Slot already cleared. Don't recurse */
	if (dbg_check_slot_free(DBG_TYPE_BREAKPOINT, slot))
		return (0);

	conf.slot = slot;
	conf.type = DBG_TYPE_BREAKPOINT;

	return (dbg_remove_xpoint(&conf));
}

static const char *
dbg_watchtype_str(uint32_t type)
{

	switch (type) {
		case DBG_WB_CTRL_EXEC:
			return ("execute");
		case DBG_WB_CTRL_STORE:
			return ("write");
		case DBG_WB_CTRL_LOAD:
			return ("read");
		case DBG_WB_CTRL_LOAD | DBG_WB_CTRL_STORE:
			return ("read/write");
		default:
			return ("invalid");
	}
}

static int
dbg_watchtype_len(uint32_t len)
{

	switch (len) {
	case DBG_WB_CTRL_LEN_1:
		return (1);
	case DBG_WB_CTRL_LEN_2:
		return (2);
	case DBG_WB_CTRL_LEN_4:
		return (4);
	case DBG_WB_CTRL_LEN_8:
		return (8);
	default:
		return (0);
	}
}

void
dbg_show_watchpoint(void)
{
	uint32_t wcr, len, type;
	uint32_t addr;
	boolean_t is_enabled;
	int i;

	if (!dbg_capable()) {
		db_printf("Architecture does not support HW "
		    "breakpoints/watchpoints\n");
		return;
	}

	db_printf("\nhardware watchpoints:\n");
	db_printf("  watch    status        type  len     address              symbol\n");
	db_printf("  -----  --------  ----------  ---  ----------  ------------------\n");
	for (i = 0; i < dbg_watchpoint_num; i++) {
		wcr = dbg_wb_read_reg(DBG_REG_BASE_WCR, i);
		if ((wcr & DBG_WB_CTRL_E) != 0)
			is_enabled = TRUE;
		else
			is_enabled = FALSE;

		type = DBG_WB_CTRL_ACCESS_MASK(wcr);
		len = DBG_WB_CTRL_LEN_MASK(wcr);
		addr = dbg_wb_read_reg(DBG_REG_BASE_WVR, i) & DBGWVR_ADDR_MASK;
		db_printf("  %-5d  %-8s  %10s  %3d  0x%08x  ", i,
		    is_enabled ? "enabled" : "disabled",
		    is_enabled ? dbg_watchtype_str(type) : "",
		    is_enabled ? dbg_watchtype_len(len) : 0,
		    addr);
		db_printsym((db_addr_t)addr, DB_STGY_ANY);
		db_printf("\n");
	}
}

static boolean_t
dbg_check_slot_free(enum dbg_t type, u_int slot)
{
	uint32_t cr, vr;
	uint32_t max;

	switch(type) {
	case DBG_TYPE_BREAKPOINT:
		max = dbg_breakpoint_num;
		cr = DBG_REG_BASE_BCR;
		vr = DBG_REG_BASE_BVR;
		break;
	case DBG_TYPE_WATCHPOINT:
		max = dbg_watchpoint_num;
		cr = DBG_REG_BASE_WCR;
		vr = DBG_REG_BASE_WVR;
		break;
	default:
		db_printf("%s: Unsupported event type %d\n", __func__, type);
		return (FALSE);
	}

	if (slot >= max) {
		db_printf("%s: Invalid slot number %d, max %d\n",
		    __func__, slot, max - 1);
		return (FALSE);
	}

	if ((dbg_wb_read_reg(cr, slot) & DBG_WB_CTRL_E) == 0 &&
	    (dbg_wb_read_reg(vr, slot) & DBGWVR_ADDR_MASK) == 0)
		return (TRUE);

	return (FALSE);
}

static u_int
dbg_find_free_slot(enum dbg_t type)
{
	u_int max, i;

	switch(type) {
	case DBG_TYPE_BREAKPOINT:
		max = dbg_breakpoint_num;
		break;
	case DBG_TYPE_WATCHPOINT:
		max = dbg_watchpoint_num;
		break;
	default:
		db_printf("Unsupported debug type\n");
		return (~0U);
	}

	for (i = 0; i < max; i++) {
		if (dbg_check_slot_free(type, i))
			return (i);
	}

	return (~0U);
}

static u_int
dbg_find_slot(enum dbg_t type, db_expr_t addr)
{
	uint32_t reg_addr, reg_ctrl;
	u_int max, i;

	switch(type) {
	case DBG_TYPE_BREAKPOINT:
		max = dbg_breakpoint_num;
		reg_addr = DBG_REG_BASE_BVR;
		reg_ctrl = DBG_REG_BASE_BCR;
		break;
	case DBG_TYPE_WATCHPOINT:
		max = dbg_watchpoint_num;
		reg_addr = DBG_REG_BASE_WVR;
		reg_ctrl = DBG_REG_BASE_WCR;
		break;
	default:
		db_printf("Unsupported debug type\n");
		return (~0U);
	}

	for (i = 0; i < max; i++) {
		if ((dbg_wb_read_reg(reg_addr, i) == addr) &&
		    ((dbg_wb_read_reg(reg_ctrl, i) & DBG_WB_CTRL_E) != 0))
			return (i);
	}

	return (~0U);
}

static __inline boolean_t
dbg_monitor_is_enabled(void)
{

	return ((cp14_dbgdscrint_get() & DBGSCR_MDBG_EN) != 0);
}

static int
dbg_enable_monitor(void)
{
	uint32_t dbg_dscr;

	/* Already enabled? Just return */
	if (dbg_monitor_is_enabled())
		return (0);

	dbg_dscr = cp14_dbgdscrint_get();

	switch (dbg_model) {
	case ID_DFR0_CP_DEBUG_M_V6:
	case ID_DFR0_CP_DEBUG_M_V6_1: /* fall through */
		cp14_dbgdscr_v6_set(dbg_dscr | DBGSCR_MDBG_EN);
		break;
	case ID_DFR0_CP_DEBUG_M_V7: /* fall through */
	case ID_DFR0_CP_DEBUG_M_V7_1:
		cp14_dbgdscr_v7_set(dbg_dscr | DBGSCR_MDBG_EN);
		break;
	default:
		break;
	}
	isb();

	/* Verify that Monitor mode is set */
	if (dbg_monitor_is_enabled())
		return (0);

	return (ENXIO);
}

static int
dbg_setup_xpoint(struct dbg_wb_conf *conf)
{
	struct pcpu *pcpu;
	struct dbreg *d;
	const char *typestr;
	uint32_t cr_size, cr_priv, cr_access;
	uint32_t reg_ctrl, reg_addr, ctrl, addr;
	boolean_t is_bkpt;
	u_int cpu;
	u_int i;

	if (!dbg_capable())
		return (ENXIO);

	is_bkpt = (conf->type == DBG_TYPE_BREAKPOINT);
	typestr = is_bkpt ? "breakpoint" : "watchpoint";

	if (is_bkpt) {
		if (dbg_breakpoint_num == 0) {
			db_printf("Breakpoints not supported on this architecture\n");
			return (ENXIO);
		}
		i = conf->slot;
		if (!dbg_check_slot_free(DBG_TYPE_BREAKPOINT, i)) {
			/*
			 * This should never happen. If it does it means that
			 * there is an erroneus scenario somewhere. Still, it can
			 * be done but let's inform the user.
			 */
			db_printf("ERROR: Breakpoint already set. Replacing...\n");
		}
	} else {
		i = dbg_find_free_slot(DBG_TYPE_WATCHPOINT);
		if (i == ~0U) {
			db_printf("Can not find slot for %s, max %d slots supported\n",
			    typestr, dbg_watchpoint_num);
			return (ENXIO);
		}
	}

	/* Kernel access only */
	cr_priv = DBG_WB_CTRL_PL1;

	switch(conf->size) {
	case 1:
		cr_size = DBG_WB_CTRL_LEN_1;
		break;
	case 2:
		cr_size = DBG_WB_CTRL_LEN_2;
		break;
	case 4:
		cr_size = DBG_WB_CTRL_LEN_4;
		break;
	case 8:
		cr_size = DBG_WB_CTRL_LEN_8;
		break;
	default:
		db_printf("Unsupported address size for %s\n", typestr);
		return (EINVAL);
	}

	if (is_bkpt) {
		cr_access = DBG_WB_CTRL_EXEC;
		reg_ctrl = DBG_REG_BASE_BCR;
		reg_addr = DBG_REG_BASE_BVR;
		/* Always unlinked BKPT */
		ctrl = (cr_size | cr_access | cr_priv | DBG_WB_CTRL_E);
	} else {
		switch(conf->access) {
		case HW_WATCHPOINT_R:
			cr_access = DBG_WB_CTRL_LOAD;
			break;
		case HW_WATCHPOINT_W:
			cr_access = DBG_WB_CTRL_STORE;
			break;
		case HW_WATCHPOINT_RW:
			cr_access = DBG_WB_CTRL_LOAD | DBG_WB_CTRL_STORE;
			break;
		default:
			db_printf("Unsupported exception level for %s\n", typestr);
			return (EINVAL);
		}

		reg_ctrl = DBG_REG_BASE_WCR;
		reg_addr = DBG_REG_BASE_WVR;
		ctrl = (cr_size | cr_access | cr_priv | DBG_WB_CTRL_E);
	}

	addr = conf->address;

	dbg_wb_write_reg(reg_addr, i, addr);
	dbg_wb_write_reg(reg_ctrl, i, ctrl);

	/*
	 * Save watchpoint settings for all CPUs.
	 * We don't need to do the same with breakpoints since HW breakpoints
	 * are only used to perform single stepping.
	 */
	if (!is_bkpt) {
		CPU_FOREACH(cpu) {
			pcpu = pcpu_find(cpu);
			/* Fill out the settings for watchpoint */
			d = (struct dbreg *)pcpu->pc_dbreg;
			d->dbg_wvr[i] = addr;
			d->dbg_wcr[i] = ctrl;
			/* Skip update command for the current CPU */
			if (cpu != PCPU_GET(cpuid))
				pcpu->pc_dbreg_cmd = PC_DBREG_CMD_LOAD;
		}
	}
	/* Ensure all data is written before waking other CPUs */
	atomic_thread_fence_rel();

	return (0);
}

static int
dbg_remove_xpoint(struct dbg_wb_conf *conf)
{
	struct pcpu *pcpu;
	struct dbreg *d;
	uint32_t reg_ctrl, reg_addr, addr;
	boolean_t is_bkpt;
	u_int cpu;
	u_int i;

	if (!dbg_capable())
		return (ENXIO);

	is_bkpt = (conf->type == DBG_TYPE_BREAKPOINT);
	addr = conf->address;

	if (is_bkpt) {
		i = conf->slot;
		reg_ctrl = DBG_REG_BASE_BCR;
		reg_addr = DBG_REG_BASE_BVR;
	} else {
		i = dbg_find_slot(DBG_TYPE_WATCHPOINT, addr);
		if (i == ~0U) {
			db_printf("Can not find watchpoint for address 0%x\n", addr);
			return (EINVAL);
		}
		reg_ctrl = DBG_REG_BASE_WCR;
		reg_addr = DBG_REG_BASE_WVR;
	}

	dbg_wb_write_reg(reg_ctrl, i, 0);
	dbg_wb_write_reg(reg_addr, i, 0);

	/*
	 * Save watchpoint settings for all CPUs.
	 * We don't need to do the same with breakpoints since HW breakpoints
	 * are only used to perform single stepping.
	 */
	if (!is_bkpt) {
		CPU_FOREACH(cpu) {
			pcpu = pcpu_find(cpu);
			/* Fill out the settings for watchpoint */
			d = (struct dbreg *)pcpu->pc_dbreg;
			d->dbg_wvr[i] = 0;
			d->dbg_wcr[i] = 0;
			/* Skip update command for the current CPU */
			if (cpu != PCPU_GET(cpuid))
				pcpu->pc_dbreg_cmd = PC_DBREG_CMD_LOAD;
		}
		/* Ensure all data is written before waking other CPUs */
		atomic_thread_fence_rel();
	}

	return (0);
}

static __inline uint32_t
dbg_get_debug_model(void)
{
	uint32_t dbg_m;

	dbg_m = ((cpuinfo.id_dfr0 & ID_DFR0_CP_DEBUG_M_MASK) >>
	    ID_DFR0_CP_DEBUG_M_SHIFT);

	return (dbg_m);
}

static __inline boolean_t
dbg_get_ossr(void)
{

	switch (dbg_model) {
	case ID_DFR0_CP_DEBUG_M_V7:
		if ((cp14_dbgoslsr_get() & DBGOSLSR_OSLM0) != 0)
			return (TRUE);

		return (FALSE);
	case ID_DFR0_CP_DEBUG_M_V7_1:
		return (TRUE);
	default:
		return (FALSE);
	}
}

static __inline boolean_t
dbg_arch_supported(void)
{
	uint32_t dbg_didr;

	switch (dbg_model) {
	case ID_DFR0_CP_DEBUG_M_V6:
	case ID_DFR0_CP_DEBUG_M_V6_1:
		dbg_didr = cp14_dbgdidr_get();
		/*
		 * read-all-zeroes is used by QEMU
		 * to indicate that ARMv6 debug support
		 * is not implemented. Real hardware has at
		 * least version bits set
		 */
		if (dbg_didr == 0)
			return (FALSE);
		return (TRUE);
	case ID_DFR0_CP_DEBUG_M_V7:
	case ID_DFR0_CP_DEBUG_M_V7_1:	/* fall through */
		return (TRUE);
	default:
		/* We only support valid v6.x/v7.x modes through CP14 */
		return (FALSE);
	}
}

static __inline uint32_t
dbg_get_wrp_num(void)
{
	uint32_t dbg_didr;

	dbg_didr = cp14_dbgdidr_get();

	return (DBGDIDR_WRPS_NUM(dbg_didr));
}

static __inline uint32_t
dgb_get_brp_num(void)
{
	uint32_t dbg_didr;

	dbg_didr = cp14_dbgdidr_get();

	return (DBGDIDR_BRPS_NUM(dbg_didr));
}

static int
dbg_reset_state(void)
{
	u_int cpuid;
	size_t i;
	int err;

	cpuid = PCPU_GET(cpuid);
	err = 0;

	switch (dbg_model) {
	case ID_DFR0_CP_DEBUG_M_V6:
	case ID_DFR0_CP_DEBUG_M_V6_1: /* fall through */
		/*
		 * Arch needs monitor mode selected and enabled
		 * to be able to access breakpoint/watchpoint registers.
		 */
		err = dbg_enable_monitor();
		if (err != 0)
			return (err);
		goto vectr_clr;
	case ID_DFR0_CP_DEBUG_M_V7:
		/* Is core power domain powered up? */
		if ((cp14_dbgprsr_get() & DBGPRSR_PU) == 0)
			err = ENXIO;

		if (err != 0)
			break;

		if (dbg_ossr)
			goto vectr_clr;
		break;
	case ID_DFR0_CP_DEBUG_M_V7_1:
		/* Is double lock set? */
		if ((cp14_dbgosdlr_get() & DBGPRSR_DLK) != 0)
			err = ENXIO;

		break;
	default:
		break;
	}

	if (err != 0) {
		db_printf("Debug facility locked (CPU%d)\n", cpuid);
		return (err);
	}

	/*
	 * DBGOSLAR is always implemented for v7.1 Debug Arch. however is
	 * optional for v7 (depends on OS save and restore support).
	 */
	if (((dbg_model & ID_DFR0_CP_DEBUG_M_V7_1) != 0) || dbg_ossr) {
		/*
		 * Clear OS lock.
		 * Writing any other value than 0xC5ACCESS will unlock.
		 */
		cp14_dbgoslar_set(0);
		isb();
	}

vectr_clr:
	/*
	 * After reset we must ensure that DBGVCR has a defined value.
	 * Disable all vector catch events. Safe to use - required in all
	 * implementations.
	 */
	cp14_dbgvcr_set(0);
	isb();

	/*
	 * We have limited number of {watch,break}points, each consists of
	 * two registers:
	 * - wcr/bcr regsiter configurates corresponding {watch,break}point
	 *   behaviour
	 * - wvr/bvr register keeps address we are hunting for
	 *
	 * Reset all breakpoints and watchpoints.
	 */
	for (i = 0; i < dbg_watchpoint_num; ++i) {
		dbg_wb_write_reg(DBG_REG_BASE_WCR, i, 0);
		dbg_wb_write_reg(DBG_REG_BASE_WVR, i, 0);
	}

	for (i = 0; i < dbg_breakpoint_num; ++i) {
		dbg_wb_write_reg(DBG_REG_BASE_BCR, i, 0);
		dbg_wb_write_reg(DBG_REG_BASE_BVR, i, 0);
	}

	return (0);
}

void
dbg_monitor_init(void)
{
	int err;

	/* Fetch ARM Debug Architecture model */
	dbg_model = dbg_get_debug_model();

	if (!dbg_arch_supported()) {
		db_printf("ARM Debug Architecture not supported\n");
		return;
	}

	if (bootverbose) {
		db_printf("ARM Debug Architecture %s\n",
		    (dbg_model == ID_DFR0_CP_DEBUG_M_V6) ? "v6" :
		    (dbg_model == ID_DFR0_CP_DEBUG_M_V6_1) ? "v6.1" :
		    (dbg_model == ID_DFR0_CP_DEBUG_M_V7) ? "v7" :
		    (dbg_model == ID_DFR0_CP_DEBUG_M_V7_1) ? "v7.1" : "unknown");
	}

	/* Do we have OS Save and Restore mechanism? */
	dbg_ossr = dbg_get_ossr();

	/* Find out many breakpoints and watchpoints we can use */
	dbg_watchpoint_num = dbg_get_wrp_num();
	dbg_breakpoint_num = dgb_get_brp_num();

	if (bootverbose) {
		db_printf("%d watchpoints and %d breakpoints supported\n",
		    dbg_watchpoint_num, dbg_breakpoint_num);
	}

	err = dbg_reset_state();
	if (err == 0) {
		err = dbg_enable_monitor();
		if (err == 0) {
			atomic_set_int(&dbg_capable_var, 1);
			return;
		}
	}

	db_printf("HW Breakpoints/Watchpoints not enabled on CPU%d\n",
	    PCPU_GET(cpuid));
}

CTASSERT(sizeof(struct dbreg) == sizeof(((struct pcpu *)NULL)->pc_dbreg));

void
dbg_monitor_init_secondary(void)
{
	u_int cpuid;
	int err;
	/*
	 * This flag is set on the primary CPU
	 * and its meaning is valid for other CPUs too.
	 */
	if (!dbg_capable())
		return;

	cpuid = PCPU_GET(cpuid);

	err = dbg_reset_state();
	if (err != 0) {
		/*
		 * Something is very wrong.
		 * WPs/BPs will not work correctly on this CPU.
		 */
		KASSERT(0, ("%s: Failed to reset Debug Architecture "
		    "state on CPU%d", __func__, cpuid));
		/* Disable HW debug capabilities for all CPUs */
		atomic_set_int(&dbg_capable_var, 0);
		return;
	}
	err = dbg_enable_monitor();
	if (err != 0) {
		KASSERT(0, ("%s: Failed to enable Debug Monitor"
		    " on CPU%d", __func__, cpuid));
		atomic_set_int(&dbg_capable_var, 0);
	}
}

void
dbg_resume_dbreg(void)
{
	struct dbreg *d;
	u_int i;

	/*
	 * This flag is set on the primary CPU
	 * and its meaning is valid for other CPUs too.
	 */
	if (!dbg_capable())
		return;

	atomic_thread_fence_acq();

	switch (PCPU_GET(dbreg_cmd)) {
	case PC_DBREG_CMD_LOAD:
		d = (struct dbreg *)PCPU_PTR(dbreg);

		/* Restore watchpoints */
		for (i = 0; i < dbg_watchpoint_num; i++) {
			dbg_wb_write_reg(DBG_REG_BASE_WVR, i, d->dbg_wvr[i]);
			dbg_wb_write_reg(DBG_REG_BASE_WCR, i, d->dbg_wcr[i]);
		}

		PCPU_SET(dbreg_cmd, PC_DBREG_CMD_NONE);
		break;
	}
}