2020-02-20  Nelson Chu  <nelson.chu@sifive.com>
 
+       * config/tc-riscv.c (struct riscv_set_options): New field csr_check.
+       (riscv_opts): Initialize it.
+       (reg_lookup_internal): Check the `riscv_opts.csr_check`
+       before doing the CSR checking.
+       (enum options): Add OPTION_CSR_CHECK and OPTION_NO_CSR_CHECK.
+       (md_longopts): Add mcsr-check and mno-csr-check.
+       (md_parse_option): Handle new enum option values.
+       (s_riscv_option): Handle new long options.
+       * doc/c-riscv.texi: Add description for the new .option and assembler
+       options.
+       * testsuite/gas/riscv/priv-reg-fail-fext.d: Add `-mcsr-check` to enable
+       the CSR checking.
+       * testsuite/gas/riscv/priv-reg-fail-rv32-only.d: Likewise.
+
        * config/tc-riscv.c (csr_extra_hash): New.
        (enum riscv_csr_class): New enum.  Used to decide
        whether or not this CSR is legal in the current ISA string.
 
   int rve; /* Generate RVE code.  */
   int relax; /* Emit relocs the linker is allowed to relax.  */
   int arch_attr; /* Emit arch attribute.  */
+  int csr_check; /* Enable the CSR checking.  */
 };
 
 static struct riscv_set_options riscv_opts =
   0,   /* rve */
   1,   /* relax */
   DEFAULT_RISCV_ATTR, /* arch_attr */
+  0.   /* csr_check */
 };
 
 static void
   if (riscv_opts.rve && class == RCLASS_GPR && DECODE_REG_NUM (r) > 15)
     return -1;
 
-  if (class == RCLASS_CSR && !reg_csr_lookup_internal (s))
+  if (class == RCLASS_CSR
+      && riscv_opts.csr_check
+      && !reg_csr_lookup_internal (s))
     return -1;
 
   return DECODE_REG_NUM (r);
   OPTION_NO_RELAX,
   OPTION_ARCH_ATTR,
   OPTION_NO_ARCH_ATTR,
+  OPTION_CSR_CHECK,
+  OPTION_NO_CSR_CHECK,
   OPTION_END_OF_ENUM
 };
 
   {"mno-relax", no_argument, NULL, OPTION_NO_RELAX},
   {"march-attr", no_argument, NULL, OPTION_ARCH_ATTR},
   {"mno-arch-attr", no_argument, NULL, OPTION_NO_ARCH_ATTR},
+  {"mcsr-check", no_argument, NULL, OPTION_CSR_CHECK},
+  {"mno-csr-check", no_argument, NULL, OPTION_NO_CSR_CHECK},
 
   {NULL, no_argument, NULL, 0}
 };
       riscv_opts.arch_attr = FALSE;
       break;
 
+    case OPTION_CSR_CHECK:
+      riscv_opts.csr_check = TRUE;
+      break;
+
+    case OPTION_NO_CSR_CHECK:
+      riscv_opts.csr_check = FALSE;
+      break;
+
     default:
       return 0;
     }
     riscv_opts.relax = TRUE;
   else if (strcmp (name, "norelax") == 0)
     riscv_opts.relax = FALSE;
+  else if (strcmp (name, "csr-check") == 0)
+    riscv_opts.csr_check = TRUE;
+  else if (strcmp (name, "no-csr-check") == 0)
+    riscv_opts.csr_check = FALSE;
   else if (strcmp (name, "push") == 0)
     {
       struct riscv_option_stack *s;
 
 Don't generate the default riscv elf attribute section if the .attribute
 directives are not set.
 
+@cindex @samp{-mcsr-check} option, RISC-V
+@item -mcsr-check
+Enable the CSR checking for the ISA-dependent CRS and the read-only CSR.
+The ISA-dependent CSR are only valid when the specific ISA is set.  The
+read-only CSR can not be written by the CSR instructions.
+
+@cindex @samp{-mno-csr-check} option, RISC-V
+@item -mno-csr-check
+Don't do CSR cheching.
 @end table
 @c man end
 
 desirable.
 @end table
 
+@item csr-check
+@itemx no-csr-check
+Enables or disables the CSR checking.
+
 @cindex INSN directives
 @item .insn @var{value}
 @itemx .insn @var{value}